Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of the new operator in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 03:45:43 10月02日 Update

This article is to share with you about the use of new operators in JavaScript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Implementation of js Simulation new operator

If you search the Nuggets for this question, you may find an answer similar to the following:

To tell you the truth, I don't understand it the first time. I need to go through the knowledge of prototype and prototype chain in order to understand. So I think MDN's interpretation of new is easier to understand:

The new operator creates an instance of a user-defined object type or of a built-in object with a constructor. The new keyword does the following:

Create an empty simple JavaScript object (that is, {})

Link this object (that is, set its constructor) to another object

Use the newly created object in step 1 as the context of the this

If the function does not return an object, it returns this.

Next, let's look at the implementation:

Function Dog (name, color, age) {this.name = name; this.color = color; this.age = age;} Dog.prototype= {getName: function () {return this.name}} var dog = new Dog ('rhubarb', 'yellow', 3)

I believe there is no need to explain the above code, everyone understands it. Let's take a look at the last line of code with the new keyword, following the above steps 1, 2, 3, and 4 to parse the operation behind new.

Step 1: create a simple empty object

Var obj = {}

Step 2: link this object to another object (prototype chain)

/ / set prototype chain obj.__proto__ = Dog.prototype

Step 3: use the newly created object in step 1 as the context of the this

/ / this points to the obj object Dog.apply (obj, ['rhubarb', 'yellow', 3])

Step 4: if the function does not return an object, it returns this

/ / because Dog () does not return a value, it returns objvar dog = objdog.getName () / / 'rhubarb'

It is important to note that if Dog () has return, it returns the value of return.

Var rtnObj = {} function Dog (name, color, age) {/ /... / returns an object return rtnObj} var dog = new Dog ('rhubarb', 'yellow', 3) console.log (dog = rtnObj) / / true

Next, we encapsulate the above steps into an object instantiation method, that is, to simulate the operation of new:

Function objectFactory () {var obj = {}; / get the first parameter of the method (and delete the first parameter), which is the constructor var Constructor = [] .shift.apply (arguments); / / point the internal property _ _ proto__ of the new object to the prototype of the constructor so that the new object can access the property and method obj.__proto__ = Constructor.prototype in the prototype / / get the return value of the constructor var ret = Constructor.apply (obj, arguments); / / return the object if the return value is an object, otherwise return an instance object of the constructor return typeof ret = = "object"? Ret: obj;} Thank you for reading! This is the end of the article on "what is the use of the new operator in JavaScript". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Object function prototype rhubarb instance operator parameter method step next up and down context code key keyword content property point more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Shulou Information NVidia Shulou Technology