The new Keyword in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
React never touches the Real DOM directly on every state change. Instead, it builds a lightweight JavaScript copy of the DOM (Virtual DOM), compares the new version against the old one (diffing/reconc

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser, primarily for building server-side and network appl

Hello guys, in this article we are going to learn about the callback functions in the js, what are they , why we use them and the problems we face while using them. Callback function A callback functi

Hi everyone , in this article we are going to explore the flat method of the Array instance, the nested arrays , how flatterning the array is useful and lot more. What are the nested arrays Nested arr
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
new Keyword Actually Do?When a function is called with the new keyword, the function will be used as a constructor.
new operator will do the following things:
It creates a new object.
It sets this new object's internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function's external, accessible, prototype object (every function object automatically has a prototype property).
It makes the this variable point to the newly created object.
It executes the constructor function, using the newly created object whenever this is mentioned.
It returns the newly created object, unless the constructor function returns a non-null object reference. In this case, that object reference is returned instead.
Note - Classes can only be instantiated with the
newoperator — attempting to call a class withoutnewwill throw aTypeError.
new with constructor functionsConstructor functions in JavaScript are regular functions designed to create and initialize multiple objects with shared properties and methods.
Creating an object with a user-defined constructor function requires two steps:
Define the object type by writing a function that specifies its name and properties. For example, a constructor function to create an object Person might look like this:
function Person(name,email){
this.name = name;
this.email.email;
}
Create an instance of the object with new.
const mukul = new Person("Mukul","mukul123@gmail.com");
new with classesclass Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
const p = new Person("Mukul");
p.greet(); // Hello, my name is Mukul
new Links PrototypesIn the object creation with the new keyword , the js perform the five steps.
Creates a brand new empty object
{} — let’s call it newObj.
Links the new object’s prototype
Sets newObj.__proto__ (or more accurately, [[Prototype]]) to Person.prototype.
Binds this to the new object
Inside the constructor function, this now refers to newObj.
Executes the constructor body
The code inside Person() runs, attaching name and email value to newObj.
Returns the new object (automatically)
No need to mention return explicitly, JavaScript returns newObj for you automatically and implicitly.
And in the new prototype linking process, every instance created with new Person() has instance.__proto__ = Person.prototype.