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
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn