Skip to content

Instantly share code, notes, and snippets.

@bbraithwaite
Created January 15, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbraithwaite/8447129 to your computer and use it in GitHub Desktop.
Save bbraithwaite/8447129 to your computer and use it in GitHub Desktop.
Some test object extensions for a blog post.
exports.utils = function () {
if ( typeof String.prototype.startsWith != 'function' ) {
String.prototype.startsWith = function( str ) {
return this.substring( 0, str.length ) === str;
}
};
if ( typeof Object.prototype.sum != 'function' ) {
Object.prototype.sum = function( ) {
var sum = 0,
that = this;
for (var i = 0; i < that.length; i++) {
sum += that[i];
}
return sum;
}
};
if ( typeof Object.prototype.any != 'function' ) {
Object.prototype.any = function( ) {
return this.length > 0;
}
};
if ( typeof Object.prototype.replaceAll != 'function' ) {
Object.prototype.replaceAll = function(a, b) {
var that = this;
while (that.indexOf(a) !== -1) {
that = that.replace(a, b);
}
return that;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment