JavaScript | new Keyword

01 Jun, 2020
New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with ‘new’ operator, the following actions are taken:
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.
A newly created object is returned when the constructor function returns a non-primitive value (custom JavaScript object). If the constructor function returns a primitive value, it will be ignored. At the end of the function, ‘this’ is returned if there is no return statement in the function body.
Syntax:
new constructorFunction(arguments)
Parameters:
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.
Example 1:
<script>  function Fruit(color, taste, seeds) {    this.color = color;    this.taste = taste;    this.seeds = seeds;}  // Create an objectconst fruit1 = new Fruit('Yellow', 'Sweet', 1);  // Display the resultdocument.write(fruit1.color);  </script>
Output:
Yellow
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().
Example 2:
<script>      function func() {    var c = 1;    this.a = 100;}  // Set the function prototypefunc.prototype.b = 200;   // Create an objectvar obj = new func();   // Display the resultdocument.write(obj.a);    document.write("\n");document.write(obj.b);   </script>
Output:
100 200

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.
javascript-basics
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
JavaScript | Promises
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?