Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.