Skip to content

Object.defineproperties does not work well with symbols #31750

@david-gang

Description

@david-gang

Hi all,

This is a continuation of the SO http://stackoverflow.com/questions/36111582/is-it-valid-to-use-object-defineproperties-with-symbols

It should be valid to define Object.defineproperties with symbols.

However the implementation at https://github.com/angular/zone.js/blob/master/lib/browser/define-property.ts#L22-L27 uses Object.keys which does not take symbols into account.

So the following code would not work well

var obj = {};
var x = Symbol();
Object.defineProperties(obj, {
  [x]: {
    value: true,
    writable: true
  },
  "property2": {
    value: "Hello",
    writable: false
  }
  // etc. etc.
});
console.log(obj[x])

It would print undefined.

This causes also problems in ngUpgraders/ng-forward#158

There is also a comment in SO which advises not to polyfill it at all if it is already defined. Is this possible?

Anyway the solution proposed there is that we should use instead of Object.keys(props)
Object.getOwnPropertyNames(props).concat(Object.getOwnPropertySymbols(props))

If the core contributors agree to this solution I could craft a PR.
Thanks,
David

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions