javascript物件導向編程代碼_js物件導向

來源:互聯網
上載者:User
複製代碼 代碼如下:

var orchard = function (){ //基底類型建構函式代理 靜態方法都在代理函數上
this.constructor && this.constructor.apply(this,arguments);
};
orchard.extend = function() {
var parentObj = this;
var parameters = parentObj.parameters ?
parentObj.parameters.concat(_.toArray(arguments)) : _.toArray(arguments);
var thisObj = function(){ //繼承類型建構函式代理
var newparameters = parameters.concat(_.toArray(arguments));
this.constructor && this.constructor.apply(this,newparameters);
};
_.extend(thisObj,parentObj);
_.extend(thisObj.prototype,parentObj.prototype);
thisObj.parameters = parameters;
thisObj.base = thisObj.prototype.base = parentObj; //基底類型的代理函數
thisObj.supper = thisObj.prototype.supper = parentObj.prototype; //基底類型的建構函式 類成員都在建構函式上
return thisObj;
};
orchard.define = function(object){
if(typeof object === "undefined") object = {constructor: function(){}};
this.prototype = object.constructor;
this.prototype.constructor = this.prototype;
for(var name in this.base)
if(typeof this[name] === "undefined")
this[name] = this.base[name];
for(var name in this.supper)
if(typeof this.prototype[name] === "undefined")
this.prototype[name] = this.supper[name];
for(var i = 0; i < arguments.length; i++)
_.extend(this.prototype,arguments[i]);
this.prototype.base = this.base;
this.prototype.supper = this.supper;
this.supper = undefined;
delete this.supper;
return this;
};
orchard.definenew = function(){
var newclass = this.extend();
return define.apply(newclass,arguments);
};

調用:
複製代碼 代碼如下:

var Person = orchard.definenew({
constructor: function(name){
this.name = name;
},
say: function(){ return "Hello, i'm " + name;}
});
var aBen = Person.extend("aBen");
aBen.define({
constructor: function(){
this.supper.apply(this,arguments);
}
});
var aben = new aBen();
alert(aben.say());

思路就是這樣的,代碼沒驗證過。分享的思路,大家自己看著辦。哈哈~~
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.