Home > Article > Web Front-end > The process of new an object in js
This article mainly introduces the process of new object in js. It has a very good reference value. Let’s take a look at it with the editor.
The specific steps of using the new keyword to call the function (new ClassA(...)):
1. Create an empty object {}
2. Use the new object and call the function. This in the function is pointed to the new instance object :
Constructor(); The constructor attribute is the name of the constructor. Set the __proto__ attribute of the new object to point to the prototype object of the constructor
4. Set the address of the new object that has been initialized , saved to the variable on the left side of the equal sign
Note: If there is no return value in the constructor or the return value is of a basic type (Number, String, Boolean) value, a new instance object is returned; if the return value is a reference type value, the actual return value is this reference type.
var foo = "bar";
function test () {
this.foo = "foo";
}
new test(); //test中的this指新对象,并未改变全局的foo属性
console.log(this.foo); // "bar"
console.log(new testThis().foo); // "foo";new和属性访问.运算符优先级相通,从左往右执行
The above is my personal understanding. If there are any errors, please leave a message to correct me.
For more articles related to the process of new an object in js, please pay attention to the PHP Chinese website!