Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the constructor in es6?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge of what the constructor in es6 refers to. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

In es6, the constructor is a special function, which is mainly used to initialize the object, that is, to attach the initial value to the object member variable. The first letter of the function name is usually capitalized and is always used with new. A function can be treated as a constructor only when called in conjunction with the new operator, and is just a normal function if the new operator is not used.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

Constructor is a special function that is mainly used to initialize objects, that is, to attach initial values to object member variables, which is always used with new. We can extract some common properties and methods from the object and encapsulate them into this function.

The first letter of the function name of the constructor is usually capitalized.

When called as a constructor, it must be used with the new operator. A function can be treated as a constructor only when called with the new operator, and is just a normal function if the new operator is not used.

When a function is used as a constructor, it can create an instance of the object through the new operator and call the corresponding function through the instance.

/ / Constructor function Person (name, age) {this.name = name; this.age = age; this.sayName = function () {alert (this.name);} var person = new Person ('kingx',' 12'); person.sayName (); / / 'kingx'

When a function is used as an ordinary function, the this inside the function points to window.

Person ('kingx',' 12'); window.sayName (); / / 'kingx'

Using the constructor, you can create the object instance we want at any time, and the constructor performs the following four steps when executed:

Create a new object through the new operator and a new address in memory.

Determines the direction for the this in the constructor.

Execute the constructor code to add properties to the instance.

Returns the newly created object.

Take the code that generated the person instance earlier as an example:

Step 1: create a new address in memory for the person instance.

Step 2: determine the this point of the person instance, pointing to person itself.

Step 3: add name, age, and sayName attributes to the person instance, where the sayName attribute value is a function.

Step 4: return the person instance.

Note: a sayName property is added to the constructor for this, whose value is a function, so that each time a new instance is created, a sayName property is added to the instance, and the sayName property is different in different instances.

That's all of the article "what does the constructor in es6 refer to?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report