- A new empty object is created.
- The new object’s internal ‘Prototype’ property (__proto__) is set the same as the prototype of the constructing function.
- The ‘this’ variable is made to point to the newly created object. It binds the property which is declared with ‘this’ keyword to the new object.
- ConstructorFunction: A class or function that specifies the type of the object instance.
- Arguments: A list of values that the constructor will be called with.
In the above example, the ‘new’ keyword creates an empty object. Here, Fruit() includes three properties ‘color’, ‘taste’, and ‘seeds’ that are declared with ‘this’ keyword. So, a new empty object will now include all these properties i.e. ‘color’, ‘taste’ and ‘seeds’. The newly created objects are returned as fruit1().
In the above example, the ‘new’ keyword creates an empty object and then sets the ‘prototype’ property of this empty object to the prototype property of func(). New property ‘b’ is assigned using func.prototype.y. So, the new object will also include ‘b’ property. Then it binds all the properties and functions declared with this keyword to a new empty object. Here, func() includes only one property ‘a’ which is declared with this keyword. So new empty object will now include ‘a’ property. The func() also includes ‘c’ variable which is not declared with this keyword. So ‘c’ will not be included in new object. Lastly, the newly created object is returned. Note that func() does not include areturn statement. The compiler will implicitly insert ‘return this’ at the end.
- How to instantiate Struct using new keyword in Golang?
- New self vs. new static in PHP
- Golang program that uses fallthrough keyword
- How to implement is functionality without using is keyword in C#
- Final keyword in PHP
- Global keyword in Python
- Invoking an overloaded constructor using this keyword in C#
- Throw Keyword in Scala
- CSS | currentcolor keyword
- PHP | var keyword
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.