descriptors and not only own properties gotcha

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Dec 17 10:09:12 PST 2014


This is mainly for clarification purpose since I need to handle these
objects in a single place.

Reading descriptors's description:
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-property-descriptor-specification-type

and reading how descriptors are retrieved:
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ordinarygetownproperty

I wonder if it will ever happen that inherited properties will be just
ignored/dropped and descriptors made safer without being able to set
properties, even by accident, on the `Object.prototype`

### Rationale & Examples
It looks like `getOwnPropertyDescriptor` will always return objects with
own properties, making considered inheritance for descriptor not only an
issue when setting them, but also when reading them.

```js
Object.prototype.get = function () {};
var descriptor = Object.getOwnPropertyDescriptor(
  Object.prototype,
  'toString'
);
// resultingo into
Object {
  value: function,
  writable: true,
  enumerable: false,
  configurable: true,
  get: function
}

// note the get is inherited
```

How can I tell if I should accept a property without passing through `value
in object || object.hasOwnProperty('get')` and similar stuff?

I'd say own properties should have priority but then why are inherited
properties considered at all if these cannot be overwritten without causing
potential problems?

As example, that descriptor that inherits from `Object.prototype` has been
retrieved via native methods, `Object.getOwnPropertyDescriptor`, and it
will **fail** through native methods.

Following an example:

```js
Object.defineProperty(Object.prototype, 'toString', descriptor);
Uncaught TypeError: Invalid property.  A property cannot both have
accessors and be writable or have a value, #<Object>message: "Invalid
property.  A property cannot both have accessors and be writable or have a
value, #<Object>"stack: (...)get stack: function () { [native code] }set
stack: function () { [native code] }__proto__: Error
```

Thanks for any sort of outcome.

Best Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141217/08f0d3b4/attachment.html>


More information about the es-discuss mailing list