Method 4 of javascript writing

Source: Internet
Author: User

4. constructor + prototype directly assemble a class. The same constructor installs the same type in the group.
The previous articles show that javascript writing classes are based on constructors and prototypes. In this case, we write a tool function to write the class.
Copy codeThe Code is as follows:
/**
* $ Class write tool function
* @ Param {Object} constructor
* @ Param {Object} prototype
*/
Function $ class (constructor, prototype ){
Var c = constructor | function (){};
Var p = prototype | {};
C. prototype = p;
Return c;
}

Well. After the tool class is written, assemble it: Use the constructor to generate the attributes (fields) of the class instance. The prototype object is used to generate the class instance method.
Copy codeThe Code is as follows:
// Constructor
Function Person (name ){
This. name = name;
}
// Prototype object
Var proto = {
GetName: function () {return this. name },
SetName: function (name) {this. name = name ;}
}

// Assemble
Var Man = $ class (Person, proto );
Var Woman = $ class (Person, proto );

OK. At this time, two classes of Man and Woman are obtained. And is of the same type. The test is as follows:
Copy codeThe Code is as follows:
Console. log (Man = Woman); // true
Console. log (Man. prototype = Woman. prototype); // true

Create an object,
Copy codeThe Code is as follows:
Var man = new Man ("Andy ");
Var woman = new Woman ("Lily ");
Console. log (man instanceof Man); // true
Console. log (woman instanceof Woman); // true
Console. log (man instanceof Person); // true
Console. log (woman instanceof Person); // true

OK. Everything is as expected. However, the following code outputs false,
Copy codeThe Code is as follows:
Console. log (man. constructor = Person); // false

This is unpleasant: From the code above, we can see that man is indeed var Man = new man ("Andy") from the new Man class, so the man constructor of the object instance should point to Man, but why is it counterproductive?
The reason is that the prototype of Person is rewritten in $ class: c. prototype = p;
Well, we will slightly rewrite $ class and link all methods to the constructor prototype (instead of rewriting the constructor prototype), as shown below:
Copy codeThe Code is as follows:
Function $ class (constructor, prototype ){
Var c = constructor | function (){};
Var p = prototype | {};
// C. prototype = p;
For (var recognition in p)
C. prototype [recognition] = p [recognition];
Return c;
}

Related Article

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.