Object.prototype.get & bye bye Object.defineProperty

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Nov 28 20:59:13 PST 2012

On Nov 28, 2012, at 6:40 PM, Andrea Giammarchi wrote:

> This was not true at some point, but now it seems to be the case in every browser.
> Just 
> Object.prototype.get = function(){};
> and any further attempt to use Object.defineProperty(obj, key, {value:123}) will fail because defineProperty checks inherited get property, or set, and these cannot be used together with value ... 
> Is this a bug or kinda a joke ?

It's what the ES5/5.1 spec says. this is the first time I've seen this raised as an issue and as far as I know it was always been that way.

It conceivably could be changed to only check own properties but I wonder if that might not break more code than that which actually has this problem.  There are at least semi-plausable reasons why you might want to intentionally use inherited properties in a property descriptor:

var defaultDataProperty = {enumerable: true, configurable: true, writable: true};

Object.defineProperty(obj,key, Object.create(defaultDataProperty, {value: 123});

Who knows whether anybody does things like this.

You can protect yourself against exposure to such Object.prototype attacks  by using patterns like:

Object.defineProperty(obj,key,Object.create(null, {value:	123};


> Scary stuff, IMHO, thanks for clarifications.
> br
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121128/4887b0e9/attachment.html>

More information about the es-discuss mailing list