1 var orchard = function (){ //基类型构造函数代理 静态方法都在代理函数上
2 this.constructor && this.constructor.apply(this,arguments);
3 };
4
5 orchard.extend = function() {
6 var parentObj = this;
7 var parameters = parentObj.parameters ?
8 parentObj.parameters.concat(_.toArray(arguments)) : _.toArray(arguments);
9 var thisObj = function(){ //继承类型构造函数代理
10 var newparameters = parameters.concat(_.toArray(arguments));
11 this.constructor && this.constructor.apply(this,newparameters);
12 };
13 _.extend(thisObj,parentObj);
14 _.extend(thisObj.prototype,parentObj.prototype);
15 thisObj.parameters = parameters;
16 thisObj.base = thisObj.prototype.base = parentObj; //基类型的代理函数
17 thisObj.supper = thisObj.prototype.supper = parentObj.prototype; //基类型的构造函数 类成员都在构造函数上
18 return thisObj;
19 };
20
21 orchard.define = function(object){
22 if(typeof object === "undefined") object = {constructor: function(){}};
23 this.prototype = object.constructor;
24 this.prototype.constructor = this.prototype;
25 for(var name in this.base)
26 if(typeof this[name] === "undefined")
27 this[name] = this.base[name];
28 for(var name in this.supper)
29 if(typeof this.prototype[name] === "undefined")
30 this.prototype[name] = this.supper[name];
31 for(var i = 0; i < arguments.length; i++)
32 _.extend(this.prototype,arguments[i]);
33 this.prototype.base = this.base;
34 this.prototype.supper = this.supper;
35 this.supper = undefined;
36 delete this.supper;
37 return this;
38 };
39
40 orchard.definenew = function(){
41 var newclass = this.extend();
42 return define.apply(newclass,arguments);
43 };
44
调用:
1 var Person = orchard.definenew({
2 constructor: function(name){
3 this.name = name;
4 },
5 say: function(){ return "Hello, i'm " + name;}
6 });
7
8 var aBen = Person.extend("aBen");
9 aBen.define({
10 constructor: function(){
11 this.supper.apply(this,arguments);
12 }
13 });
14
15 var aben = new aBen();
16 alert(aben.say());
思路就是这样的,代码没验证过。分享的思路,大家自己看着办。哈哈~~