|
<html>
<head>
<script type="text/Javascript"><!--
ClassModel = //類模型,用于創(chuàng)建類
{
create: function()
{
return function(){this.construct.apply(this,arguments);}
}
}
Extend = function(desc, src) //模擬類繼承, 將一個對象的所有成員 復(fù)制到 另一個對象中
{
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>
JavaScript技術(shù):javascript 面向?qū)ο笏枷?附源碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。