Part 1,
Constructor + prototype assemble a class. The same constructor can define multiple types.
View sourceprint? 01 /**
02 * $ class write tool function 2
03 * @ param {Function} constructor
04 * @ param {Object} prototype
05 */
06 function $ class (constructor, prototype ){
07 var c = constructor | function (){};
08 var p = prototype | | {};
09 return function (){
10 for (var recognition in p ){
11 arguments. callee. prototype [recognition] = p [recognition];
12}
13 c. apply (this, arguments );
14}
15}
Similar to the previous article, constructors and prototype objects are still used to define two classes.
View sourceprint? 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 // write two classes
11 var Man = $ class (Person, proto );
12 var Woman = $ class (Person, proto );
Unlike the previous article, although Man and Woman are both assembled using Person and proto. But Man is not equal to Woman. That is, the same Constructor (Person) defines different classes.
View sourceprint? 1 // The same Constructor (Person) defines different classes
2 console. log (Man = Woman); // false