最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js模擬類繼承小例子

 更新時間:2010年07月17日 18:49:17   作者:  
使用js模擬類繼承小例子,學習js面向?qū)ο蟮呐笥芽梢詤⒖枷隆?/div>
復制代碼 代碼如下:

//使用原型繼承,中間使用臨時對象作為Child的原型屬性,臨時對象的原型屬性再指向父類的原型,
//防止所有子類和父類原型屬性都指向通一個對象.
//這樣當修改子類的原型屬性,就不會影響其他子類和父類
function extend(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.base = Parent.prototype;
}

function Parent(name)
{
this.aa = 123;
this.getName = function() {return name;}; //使用閉包模擬私有成員
this.setName = function(value){name=value;};
}
Parent.prototype.print = function(){alert("print!");};
Parent.prototype.hello = function()
{
alert(this.getName() + "Parent")
};

function Child(name,age)
{
Parent.apply(this, arguments);//調(diào)用父類構(gòu)造函數(shù)來繼承父類定義的屬性
this.age = age;
}
extend(Child,Parent); //繼承Parent

Child.prototype.hello = function() //重寫父類hello方法
{
alert(this.getName() + "Child");

Parent.prototype.hello.apply(this,arguments); //調(diào)用父類同名方法
};
//子類方法
Child.prototype.doSomething = function(){ alert(this.age + "Child doSomething"); };

var p1 = new Child("xhan",22);

var p2 = new Child("xxx",33);

p1.hello();
p2.hello();

p1.doSomething(); //子類方法
p1.print(); //父類方法

alert(p1 instanceof Child); //true
alert(p1 instanceof Parent);//true

相關文章

最新評論

吉水县| 西青区| 竹山县| 乐亭县| 涞源县| 高碑店市| 石首市| 永城市| 庆安县| 蓬莱市| 平昌县| 恩平市| 建瓯市| 桐城市| 垣曲县| 探索| 西乡县| 城步| 天门市| 尤溪县| 万年县| 海安县| 穆棱市| 科技| 榆中县| 两当县| 衡阳县| 沙河市| 衡阳市| 盐池县| 广州市| 巴林右旗| 昆明市| 卓资县| 东兴市| 论坛| 平江县| 比如县| 东至县| 札达县| 乌拉特后旗|