descriptors and not only own properties gotcha

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Dec 17 10:30:03 PST 2014


Actually simplifying as proposal: does everyone agree that if a
descriptor.[[Value]] is own property no further checks on inherited [[Get]]
and [[Set]], or generally speaking Accessor Property related checks, should
be made ?

This would already fix the `Object.prototype.get` or `.set` case since I
believe nobody ever used an inherited `value` property on purpose for
descriptors ... or is it?

It will keep `enumerable` and `writable` still potentially problematic but
at least the following code would never fail:

```js
Object.prototype.get = function () {}; Object.defineProperty(
Object.prototype, 'toString', Object.getOwnPropertyDescriptor(
Object.prototype, 'toString' ) );

```

Thanks again for any sort of outcome (or clarification on when and if this
will ever be fixed)

On Wed, Dec 17, 2014 at 6:09 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:
>
> 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/d0f73aef/attachment.html>


More information about the es-discuss mailing list