Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify creation of objects with __proto__ property #565

Open
overlookmotel opened this issue Dec 22, 2023 · 0 comments
Open

Simplify creation of objects with __proto__ property #565

overlookmotel opened this issue Dec 22, 2023 · 0 comments
Labels
enhancement Improvements

Comments

@overlookmotel
Copy link
Owner

overlookmotel commented Dec 22, 2023

Input:

const obj = {
  ['__proto__']: {x: 123}
};
assert(obj.x === undefined);
assert(Object.getPrototypeOf(obj) === Object.prototype);
assert(Object.keys(obj)[0] === '__proto__');
export default obj;

Current output:

export default Object.defineProperty({}, "__proto__", {
  value: {x: 123},
  writable: true,
  enumerable: true,
  configurable: true
});

This verbose output was to avoid outputting {__proto__: {x: 1}}, which would set the prototype of the object, not a property called '__proto__'. But wrapping __proto__ as ['__proto__'] achieves the same effect and is shorter.

It would also allow removing some code which handles this special case in the serializer.

NB: When setting a circular property, Object.defineProperties() is still needed - obj.__proto__ = obj or Object.assign(obj, {['__proto__']: obj}) both trigger the setter on Object.prototype.__proto__, which results in setting the prototype.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements
Projects
None yet
Development

No branches or pull requests

1 participant