5. Define a class with a constructor + prototype; The same constructor can define multiple types
Copy Code code as follows:
/**
* $define Write the Second class tool function
* @param {Object} constructor
* @param {Object} prototype
*/
function $define (constructor,prototype) {
var c = Constructor | | function () {};
var p = Prototype | | {};
return function () {
For (Var ATR in P)
ARGUMENTS.CALLEE.PROTOTYPE[ATR] = P[atr];
C.apply (this,arguments);
}
}
Similar to the fourth approach, we still use constructors, prototype objects, and define two classes.
Copy Code code as follows:
//constructor
Function person (name) {
THIS.name = name;
}
//Prototype Object
var proto = {
Getname:function () {return this.name},
Setname:function (name) {THIS.N AME = name;
}
//define two classes
var man = $define (Person,proto);
var Woman = $define (Person,proto);
Console.log (man = = Woman);//false, the same constructor (person) defines a different class