JavaScript new keyword is used to create an instance of an object that has a constructor function.
On calling the constructor function with the '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 that is declared with the 'this' keyword to the new object.
- About the returned value, there are three situations below.
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.
Return Value:
- If the constructor function returns a non-primitive value (Object, array, etc), the constructor function still returns that value. Which means the new operator won't change the returned value.
- If the constructor function returns nothing, 'this' is returned;
- If the constructor function returns a primitive value, it will be ignored, and 'this' is returned.
Example 1: This example shows the use of a new keyword.
javascript
function Fruit(color, taste, seeds) {
this.color = color;
this.taste = taste;
this.seeds = seeds;
}
// Create an object
const fruit1 = new Fruit('Yellow', 'Sweet', 1);
// Display the result
console.log(fruit1.color);
Output:
Yellow
Explanation: 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: This example shows the use of a new keyword.
javascript
function func() {
let c = 1;
this.a = 100;
}
// Set the function prototype
func.prototype.b = 200;
// Create an object
let obj = new func();
// Display the result on console
console.log(obj.a);
console.log(obj.b);
Output:
100
200
Explanation: 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.
Similar Reads
What is the new keyword in JavaScript ? In this article, we will understand exactly what the new keyword is, and why we use the new keyword, and how we can use the new keyword. The new keyword in JavaScript: The new keyword is used to create an instance of a user-defined object type and a constructor function. It is used to construct and
2 min read
Dart - Keywords Dart Keywords are the reserved words in Dart programming Language which has some special meaning to the compiler. These keywords are case-sensitive and cannot be used to name variables, classes, and functions. There are in total 61 keywords in Dart programming language: Â Keywords Tableabstract
15+ min read
What is Keyword in Programming? Keywords are predefined or reserved words that have special meaning to the compiler. In this article, we will discuss about keywords in programming, its importance, and usage in different languages. What is a Keyword?A keyword is a reserved word in a programming language that has a predefined meanin
7 min read
How to Start Learning Java? Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
8 min read
How to Start Learning Java? Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
8 min read
How to Start Learning Java? Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
8 min read