In fact due to the languages prototypical behavior any function can be used as a constructor call, which adds more fuel to the confusion.
To demystify the process that occurs this article will take you through the four operations that occur when the
new keyword is invoked on a Function.
After describing these steps we will then work through codifying a user-land function that mimics its’ behavior.
- It creates a plain ol’ object
- It assigns the supplied Functions
prototypeobject property as the new objects
[[prototype]]link. All Functions are created with a prototype object property which initially just has a constructor member which links back to the Function itself.
- It invokes the Function constructor (with supplied arguments), binding
thisto the newly created object.
- It checks to see if a non-primitive value has been returned from the function call, if so it will disregard the created object and return this, else the newly formed object is returned.
Now that we know the basic steps that occur when the
new keyword is called, we can create a user-land function that mimic this behavior.