Javascript Writing Method (2) -- convert

Source: Internet
Author: User

Transferred from:Http://www.cnblogs.com/snandy/archive/2011/03/06/1972254.html

 

At the beginning of this article, we will record some tool functions for writing classes. Through the previous article, we know that they are essentially constructors + prototypes. I am not afraid to understand the various writing methods it encounters.

 

Constructor + prototype directly assemble a class; the same constructor will group the same type

01 /**
02  * $ Class write tool function
03  * @param {Function} constructor
04  * @param {Object} prototype
05  */
06 function $class(constructor,prototype) {
07     var c = constructor || function(){};
08     var p = prototype || {};
09     c.prototype = p;
10     return c;
11 }

The constructor is used to generate the attributes (fields) of the class instance. The prototype object is used to generate the class instance method.

01 // Constructor
02 function Person(name) {
03     this.name = name;
04 }
05 // Prototype object
06 var proto = {
07     getName : function(){return this.name},
08     setName : function(name){this.name = name;} 
09 }
10 // Assemble
11 var Man = $class(Person,proto);
12 var Woman = $class(Person,proto);

At this time, two classes of man and woman are obtained. And is of the same type. The test is as follows:

1 console.log(Man == Woman); //true
2 console.log(Man.prototype == Woman.prototype); //true

Create an object

1 var man = new Man("Andy");
2 var woman = new Woman("Lily");
3   
4 console.log(man instanceof Man); //true
5 console.log(woman instanceof Woman); //true
6 console.log(man instanceof Person); //true
7 console.log(woman instanceof Person); //true

OK. Everything is as expected. However, the following code outputs false.

1 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:

1 function $class(constructor,prototype) {
2     var c = constructor || function(){};
3     var p = prototype || {};
4 //  c.prototype = p;
5     for(var atr in p){
6         c.prototype[atr] = p[atr];
7     }   
8     return c;
9 }

 

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.