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

How to use the constructor property in JavaScript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use the constructor attribute in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The constructor property always points to the constructor that created the current object. For example, the following example:

/ equivalent to var foo = new Array (1, 56, 34, 12); var arr = [1, 56, 34, 12]; console.log (arr.constructor = Array); / / true / / equivalent to var foo = new Function (); var Foo = function () {}; console.log (Foo.constructor = Function); / / true / / an obj object var obj = new Foo () instantiated by the constructor; console.log (obj.constructor = = Foo) / / true / / combine the above two pieces of code to get the following conclusion: console.log (obj.constructor.constructor = = Function); / / true

But when constructor encounters prototype, something interesting happens.

We know that every function has a default property prototype, and the constructor of this prototype points to this function by default. As shown in the following example:

Function Person (name) {this.name = name;}; Person.prototype.getName = function () {return this.name;}; var p = new Person ("ZhangSan"); console.log (p.constructor = Person); / / true console.log (Person.prototype.constructor = Person); / / true / / merge the first two lines of code to get the following result console.log (p.constructor.prototype.constructor = = Person); / / true

At that time, when we redefined the prototype of the function (note: unlike the previous example, it is not modified but overwritten here), the behavior of the constructor property is a bit strange, as shown in the following example:

Function Person (name) {this.name = name;}; Person.prototype = {getName: function () {return this.name;}}; var p = new Person ("ZhangSan"); console.log (p.constructor = = Person); / / false console.log (Person.prototype.constructor = = Person); / / false console.log (p.constructor.prototype.constructor = = Person); / / false

Why?

It turns out that when overwriting Person.prototype, it is equivalent to doing the following code operations:

Person.prototype = new Object ({getName: function () {return this.name;}})

The constructor attribute always points to the constructor that created itself, so at this point Person.prototype.constructor = = Object, that is:

Function Person (name) {this.name = name;}; Person.prototype = {getName: function () {return this.name;}}; var p = new Person ("ZhangSan"); console.log (p.constructor = = Object); / / true console.log (Person.prototype.constructor = = Object); / / true console.log (p.constructor.prototype.constructor = = Object); / / true

How to fix this problem? The method is also simple, just override the Person.prototype.constructor again:

Function Person (name) {this.name = name;}; Person.prototype = new Object ({getName: function () {return this.name;}}); Person.prototype.constructor = Person; var p = new Person ("ZhangSan"); console.log (p.constructor = = Person); / / true console.log (Person.prototype.constructor = Person); / / true console.log (p.constructor.prototype.constructor = Person) / / true the content of "how to use the constructor attribute in JavaScript" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report