我的javascript面向对象编程
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 319
1varorchard=function(){//基类型构造函数代理静态方法都在代理函数上2this.constructor&&this.constructor.apply(this,a
 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());

思路就是这样的,代码没验证过。分享的思路,大家自己看着办。哈哈~~

联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2