I recently read the MDN documentation on the new
operator, and I was struck by the concise description of what it does:
When the code new Foo(...) is executed, the following things happen:
- A new object is created, inheriting from Foo.prototype.
- The constructor function Foo is called with the specified arguments and this bound to the newly created object.
new Foo
is equivalent tonew Foo()
, i.e. if no argument list is specified, Foo is called without arguments.- The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
It seems like none of those things are privileged operations, so is it possible to completely recreate the action of new
with other language constructs?
Note that I don't count Reflect.construct
since the very definition of it is that it "acts like the new operator as a function".
Aucun commentaire:
Enregistrer un commentaire