A simple function to extend native object
void extendNative(Function constructor, optional String property, Object methods);
In order to prevent collisions with future standard all extensions are added under a specific property ($
by default), this is configurable by the optional second argument:
extendNative(String, {
someExtension: function() { }
});
'my string'.$.someExtension();
extendNative(String, 'extensions', {
anotherExtension: function() { }
});
'my string'.extensions.anotherExtension();
extendNative(String, '_', {
yetAnotherExtension: function() { }
});
'my string'._.yetAnotherExtension();
Inside the method you have the native value on the this
variable:
extendNative(String, {
startWith: function(start) {
var beginning = this.substr(0, start.length);
return beginning === start;
},
});
And to access extended methods you'll need to access the property:
extendNative(String, {
startWith: function(start) { ... },
notStartWith: function(start) {
return !this.$.startWith(start);
}
});