Object.prototype.get & bye bye Object.defineProperty

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Nov 29 09:27:21 PST 2012


I really don't understand what is exactly the advantage of considering
inherited properties for descriptors.

If you create a new instance with those properties using a "class" to avoid
writing them ... what are you gaining against a literal object?

If you are doing this in order to have less code so that you can `new
EnumerableWithValue(value)` why don't you

function EnumerableWithValue(value) {
  return {enumerable:true, value:value};
}

If you want to reuse objects in order to avoid greedy GC operations, why
don't you do the way I do which is to create a single object once and
update its value during definition?

var descriptor = {value: null};

function withValue(value) {
  descriptor.value = value;
  return descriptor;
}

Object.defineProperty(obj, "prop", withValue(123));

The reason I have discovered this problem is that I was trying to sandbox
an enriched Object.prototype in order to implement get(key) / set(key,
value) environment to show an example for the other discussion.

When I've developed polpetta for node.js I remember I could not use
inherited properties then, as shown here, I've realized I don't need them
at all.

Now even node.js consider inherited properties with defineProperty so I
have double checked and realized that's how it should be accordingly with
specs but I believe this is a problem

Descriptors are small enough to justify the usage of inheritance for them,
imho, also in this way is too easy to break everything because accidentally
some script polluted the Object.prototype with a value method, as example
... or an enumerable: true.

Correct, setting Object.prototype.enumerable = true; will make a whole
environment enumerable for all those properties where the descriptor has no
default because by default specs say enumerable should be false.

Then we have configurable and writable so this is in my opinion a massive
security hole in the defineProperty logic.

Object.prototype.writable = Object.prototype.enumerable =
Object.prototype.configurable = true;

Object.defineProperty(this, "notASecret", {value:"writable"});

for (var key in this) if(key === notASecret) alert("WTF"); // will show it

var descriptor = Object.getOwnPropertyDescriptor(this, "notASecret");
alert(descriptor.writable + descriptor.enumerable +
descriptor.configurable); // is 3, should be zero

br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121129/b89c59a7/attachment.html>


More information about the es-discuss mailing list