Quantcast
Channel: Comments on: Rethinking JavaScript Object Enumeration
Viewing all articles
Browse latest Browse all 14

By: Robert Biggs

$
0
0

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 all articles
Browse latest Browse all 14

Trending Articles