paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 387 forks source link

Object symbols with defineProperty(ies) on node<6 #444

Closed Xotic750 closed 6 years ago

Xotic750 commented 6 years ago

On node<6, the following code fails with TypeError: Cannot convert object to primitive value

var s = Object(Symbol(''));
var o = {}
o[s] = 1;

I know this is not fixable, however I thought that with ES6-shim loaded, that the following would work.

Object.defineProperty({}, Object(Symbol('')), { value: 1 });

but it also fails. TypeError: Cannot convert object to primitive value

ToPropertyKey works fine, converting the symbol object to symbol primitive, so Object.defineProperty can shimmed with the use of ToPropertyKey to provide the expected result.

This is also true for hasOwnProperty, because as it stands, while not throwing an error, this behaviour is observed.

var s = Symbol('');
var o = {};o[s] = 1;
Object.prototype.hasOwnProperty(o, Object(s)); // false

and again can be fixed with ToPropertyKey.

ljharb commented 6 years ago

Absolutely obj[key] = value and Object.defineProperty(obj, key, { value }) should behave the same, that doesn't surprise me.

Primarily this looks like an engine bug that simply can not be fixed.

es6-shim doesn't shim Object.defineProperty - that's ES5, and object-symbol support can't be shimmed. You're right that es6-shim could try to detect this case, but we'd end up creating an inconsistency between object brackets and Object.defineProperty.

I'd recommend running any maybe-symbols through es-to-primitive, if you want to work around this bug.

Xotic750 commented 6 years ago

I know that it's edge case, but I thought it worth a mention. ;)