Javascript's `new` keyword
·1 min
Usage #
new is used to create new instances of objects with the help of constructor functions. While taking a look
at prototypes, I ran into the new keyword, and how it relates to the function.prototype
property. I thought that it might be a good exercise to look into how new works.
Under the hood #
According to MDN, there
are three steps taken when using new:
- A new object is created. If the constructor function has its
prototypeproperty set to some object, the newly created object will inherit from that prototype. - The constructor function is called with the new object bound to
this. - The result of the
new ...expression will be what ever is returned by the constructor function. If nothing is returned, our newly created object (bound tothis) will be used instead. It is usually the case that nothing is explicitly returned from constructor functions, but we have the option to do so.
Noteworthy #
If the constructor has no parameters, new Foo will be equal to new Foo().
Example #
const myPrototype = {
protoProperty = "protolicious!"
}
function MyConstructor() {
this.message = "hello"
}
MyConstructor.prototype = myPrototype
const myInstance = new MyConstructor
myInstance.message // "hello"
myInstance.protoProperty // "protolicious!"