javascript 面向?qū)ο笏枷?附源碼
更新時(shí)間:2009年07月07日 01:40:28 作者:
javascript 面向?qū)ο笏枷雽?shí)例代碼,大家可以參考下。
復(fù)制代碼 代碼如下:
<html>
<head>
<script type="text/javascript"><!--
ClassModel = //類模型,用于創(chuàng)建類
{
create: function()
{
return function(){this.construct.apply(this,arguments);}
}
}
Extend = function(desc, src) //模擬類繼承, 將一個(gè)對(duì)象的所有成員 復(fù)制到 另一個(gè)對(duì)象中
{
for(var c in src)
{
desc[c] = src[c];
}
return desc;
}
Object.prototype.extend = function(src)
{
return Extend.apply(this, [this, src]);
}
var human = ClassModel.create();
human.prototype =
{
construct : function() //構(gòu)造函數(shù)
{
//alert("construct method");
//alert(this.speak() + "," + this.sleep());
},
speak : function()
{
alert("speak");
},
sleep : function()
{
alert("sleep");
},
sex : function()
{
alert("女");
}
}
var h = new human();
h.speak(); //調(diào)用human類的speak方法
var student = ClassModel.create();
student.prototype = (new human()).extend({ //student類繼承類human類
sex : function() //方法重載 (多態(tài))
{
alert("男");
},
study : function()
{
alert("studying");
},
thinking : function()
{
alert("thinking");
}
});
var student = new student();
student.sleep(); //調(diào)用 父類(human) 的sleep方法
student.study(); //調(diào)用 student的study方法
student.thinking(); //調(diào)用 student的thinking方法
student.sex(); //結(jié)果為 男 不再是父類的 女
// --></script>
</head>
</html>
相關(guān)文章
XRegExp 0.2: Now With Named Capture
XRegExp 0.2: Now With Named Capture...2007-11-11
javascript中的對(duì)象創(chuàng)建 實(shí)例附注釋
為了讓你的js代碼更加的專業(yè)與代碼的條理性,很多情況下都是定義成對(duì)象的方式來書寫代碼,想深入的朋友可以參考下。2011-02-02
javascript 模式設(shè)計(jì)之工廠模式學(xué)習(xí)心得
接口的實(shí)現(xiàn),從而使不同子類可以被同等的對(duì)待,恰當(dāng)?shù)氖褂霉S模式,但不要拘泥與形式,理解本質(zhì)。2010-04-04
javascript中類的定義及其方式(《javascript高級(jí)程序設(shè)計(jì)》學(xué)習(xí)筆記)
javascript也是一種面向?qū)ο蟮木幊陶Z言。但是javascript中的類相關(guān)的東西(類的定義,原型鏈,繼承等)卻不是很好理解,特別是繼承。2011-07-07
new fun的執(zhí)行過程分析,學(xué)習(xí)面向?qū)ο蟮呐笥芽梢詤⒖枷隆?/div> 2010-08-08
JavaScript 繼承機(jī)制的實(shí)現(xiàn)(待續(xù))
JavaScript繼承機(jī)制的實(shí)現(xiàn),后期會(huì)有一些補(bǔ)充。2010-05-05最新評(píng)論

