js模擬類繼承小例子
更新時(shí)間:2010年07月17日 18:49:17 作者:
使用js模擬類繼承小例子,學(xué)習(xí)js面向?qū)ο蟮呐笥芽梢詤⒖枷隆?/div>
//使用原型繼承,中間使用臨時(shí)對象作為Child的原型屬性,臨時(shí)對象的原型屬性再指向父類的原型,
//防止所有子類和父類原型屬性都指向通一個(gè)對象.
//這樣當(dāng)修改子類的原型屬性,就不會(huì)影響其他子類和父類
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
復(fù)制代碼 代碼如下:
//使用原型繼承,中間使用臨時(shí)對象作為Child的原型屬性,臨時(shí)對象的原型屬性再指向父類的原型,
//防止所有子類和父類原型屬性都指向通一個(gè)對象.
//這樣當(dāng)修改子類的原型屬性,就不會(huì)影響其他子類和父類
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
相關(guān)文章
js 頁面?zhèn)鲄?shù)時(shí) 參數(shù)值含特殊字符的問題
解決方法就是利用js的escape函數(shù),這個(gè)函數(shù)在解決中文亂碼等方面應(yīng)用的比較廣泛。推薦使用。2009-12-12微信小程序—setTimeOut定時(shí)器的問題及解決
這篇文章主要介紹了微信小程序—setTimeOut定時(shí)器的問題及解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07TypeScript中定義變量方式以及數(shù)據(jù)類型詳解
TypeScript支持 JavaScript的所有語法和語義,同時(shí)通過作為ECMAScript的超集來提供一些額外的功能,如類型檢測和更豐富的語法,這篇文章主要給大家介紹了關(guān)于TypeScript中定義變量方式以及數(shù)據(jù)類型詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08JavaScript表格隔行變色和Tab標(biāo)簽頁特效示例【附j(luò)Query版】
這篇文章主要介紹了JavaScript表格隔行變色和Tab標(biāo)簽頁特效,結(jié)合實(shí)例形式分析了javascript元素遍歷、事件響應(yīng)相關(guān)操作技巧,并附帶jQuery對應(yīng)實(shí)現(xiàn)代碼供大家參考,需要的朋友可以參考下2019-07-07JS特權(quán)方法定義作用以及與公有方法的區(qū)別
在構(gòu)造函數(shù)內(nèi)部通過this關(guān)鍵字定義的的方法為特權(quán)方法它的作用為在構(gòu)造函數(shù)外面公開訪問(僅限于實(shí)例化的對象),而且還能夠訪問私有成員和方法,感興趣的你可以參考下哈2013-03-03基于 D3.js 繪制動(dòng)態(tài)進(jìn)度條的實(shí)例詳解
D3是一個(gè)被數(shù)據(jù)驅(qū)動(dòng)的文檔。這篇文章主要介紹了基于 D3.js 繪制動(dòng)態(tài)進(jìn)度條的方法,需要的朋友可以參考下2018-02-02