5. Use the constructor prototype to define a class; the same constructor can define multiple types
/**
* $define class writing tool function 2
* @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 method, two classes are still defined using constructors and prototype objects.
//Constructor
function Person(name) {
this.name = name;
}
//Prototype object
var proto = {
getName : function(){return this.name},
setName : function( name){this.name = name;}
}
//Define two classes
var Man = $define(Person,proto);
var Woman = $define(Person,proto) ;
console.log(Man == Woman); //false, the same constructor (Person) defines different classes