Home  >  Article  >  Web Front-end  >  The process of new an object in js

The process of new an object in js

高洛峰
高洛峰Original
2017-02-21 14:34:131287browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn