Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the constructor property in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 11:52:53 10月01日 Update

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!

Tags: Properties functions code pointing equivalence content objects more knowledge practical interesting learned next things examples dilemmas examples actual situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Xiaomi OPPO Reno Apple