Quantcast
Viewing latest article 4
Browse Latest Browse All 14

By: Robert Biggs

This is a little late, but there is an easy way to extend native objects in JavaScript taking advantage of ECMAScript 5:

extend = function(obj, prop) {
Object.keys(prop).forEach(function(val) {
if (prop.hasOwnProperty(val)) {
Object.defineProperty(obj, val, {
value: prop[val],
writable: true,
enumerable: false,
configurable: true
});
}
});
};

This would allow you to extend an object’s prototype using the ‘enumerable’ property set to false so that the object’s prototype only iterates it own properties, solving that headache.

extend(Object.prototype, {
whatever : function(blah) {
// do stuff with blah
}
});


Viewing latest article 4
Browse Latest Browse All 14

Trending Articles