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
[[Prototype]] of namespace objects should be null #3096
Comments
I'm a little unsure about this one. To my knowledge, messing with
That being said, the only safe way of doing this that I am aware of is using var ns = /*#__PURE__*/Object.freeze(Object.create(null, {
x: {value: x, writable: true, enumerable: true}
})); which quickly becomes really wordy (note that we need the var ns = /*#__PURE__*/(function() {
var ns = Object.create(null);
ns.x = x;
Object.freeze(ns);
return ns;
} ()) The problem with treeshakeability here is that Rollup does not know that the assignment I am open to more suggestions but I do not think the either of the approaches are really nice, maybe the second a little nicer than the first. Of course we could also put this behind an option like the And please tell me if I miss something here! |
You're right that mutating the prototype of an object after it has been created/used is a bad idea. In this case, the null prototype is part of the declarative structure of the object from the start. So there is no dynamic mutation going on. I like your cautiousness though, so will try to get a second opinion from V8 team. |
That would be awesome. Meanwhile, I keep learning nice hacks such as that |
V8 team member here! I can confirm the nuance that @robpalme pointed out above:
A more detailed reference is JavaScript engine fundamentals: optimizing prototypes, which concludes:
|
Thanks @mathiasbynens, and nice article by the way. I already started using the pattern {
__proto__: null,
otherProperties: someValues
} in #3068 because it is just so convenient. I assume that it is still a good idea to put the On a side-note, from your article I assume that switching the prototype via const proto = {};
const obj = Object.create(proto);
proto.x = 'foo'; ? |
The PR here (add proto null) made bug when mixed named and default export🤔 Should I just remove named export from pollyfill or I can have flag to control behaviours? |
How Do We Reproduce?
Issue can be seen in the repl.
Expected Behavior
ECMAScript spec defines ES module namespaces as exotic objects and says that their
[[Prototype]]
should benull
.Actual Behavior
Namespace objects generated by Rollup contains
Object.prototype
.Rollup output runs without any errors.
If same code is ran without bundling it with Rollup, it produces a
TypeError
, which is the expected behaviour according to the spec.Using
__proto__: null
in namespaces would fix this. I would be happy to work on this if you also agree that Rollup output should be as spec compliant as possible.The text was updated successfully, but these errors were encountered: