Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created September 6, 2012 18:20
Show Gist options
  • Save james2doyle/3659152 to your computer and use it in GitHub Desktop.
Save james2doyle/3659152 to your computer and use it in GitHub Desktop.
Prototypes
Object.prototype.each = function(callback) {
var a = [];
for (var i = 0; i < this.length; i++) {
a[i] = this[i];
callback(a[i]);
}
}
String.prototype.replaceAll = function(str1, str2, ignore) {
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
}
String.prototype.toInt = function() {
var a = new Array();
for (var i = 0; i < this.length; i++) {
a[i] = this.charCodeAt(i);
}
return a;
}
Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))];
}
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment