JavaScript class and inheritance: constructor attributes

Source: Internet
Author: User

The constructor attribute always points to the constructor that creates the current object. For example:

 
 
  1. // Equivalent to var foo = new Array (1, 56, 34, 12 ); 
  2. Var arr = [1,56,34,12];
  3. Console. log (arr. constructor === Array );// True 
  4. // Equivalent to var foo = new Function (); 
  5. Var Foo = function (){};
  6. Console. log (Foo. constructor === Function );// True 
  7. // Instantiate an obj object by the constructor 
  8. Var obj =NewFoo ();
  9. Console. log (obj. constructor === Foo );// True 
  10.  
  11. // Combine the above two pieces of code to get the following conclusion: 
  12. Console. log (obj. constructor. constructor === Function );// True 

But when constructor encounters prototype, interesting things happen.

We know that each function has a default prototype attribute, and the constructor of this prototype points to this function by default. For example:

 
 
  1. Function Person (name ){
  2. This. Name = name;
  3. };
  4. Person. prototype. getName = function (){
  5. Return This. Name;
  6. };
  7. Var p =NewPerson ("ZhangSan");
  8.  
  9. Console. log (p. constructor === Person );// True 
  10. Console. log (Person. prototype. constructor = Person );// True 
  11. // Merge the above two lines of code to obtain the following results: 
  12. Console. log (p. constructor. prototype. constructor = Person );// True 

At that time, when we re-define the prototype of the function, note: the difference with the previous example is that it is not modified but overwritten). The behavior of the constructor attribute is a bit strange, as shown in the following example:

 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = {  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. };  
  9. var p = new Person("ZhangSan");  
  10. console.log(p.constructor === Person);  // false  
  11. console.log(Person.prototype.constructor === Person); // false  
  12. console.log(p.constructor.prototype.constructor === Person); // false 

Why?

It turns out that when people. prototype is overwritten, it is equivalent to performing the following code operations:

 
 
  1. Person.prototype = new Object({  
  2.     getName: function() {  
  3.         return this.name;  
  4.     }  
  5. }); 

The constructor attribute always points to the creation of its own constructor, so at this time Person. prototype. constructor = Object, that is:

 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = {  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. };  
  9. var p = new Person("ZhangSan");  
  10. console.log(p.constructor === Object);  // true  
  11. console.log(Person.prototype.constructor === Object); // true  
  12. console.log(p.constructor.prototype.constructor === Object); // true 

How can we fix this problem? The method is also very simple. Just overwrite Person. prototype. constructor again:

 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = new Object({  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. });  
  9. Person.prototype.constructor = Person;  
  10. var p = new Person("ZhangSan");  
  11. console.log(p.constructor === Person);  // true  
  12. console.log(Person.prototype.constructor === Person); // true  
  13. console.log(p.constructor.prototype.constructor === Person); // true 
  1. JavaScript class and inheritance: prototype attribute
  2. JavaScript class and inheritance: this property
  3. Summary of three + 1 implementation methods of ExtJS Grid Tooltip
  4. Implementation of JavaScript asynchronous call framework chain
  5. JQuery-style chained call of JavaScript asynchronous call framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.