javascript
javascript中的继承方式
javascript中的繼承方式有好幾種。
下面分別舉例供大家參考學(xué)習(xí):
1.function parent()
{
? this.x=1;
}
function child()
{
?? var instance=new parent();//實(shí)例化父類(lèi)
?? for(var i in instance)
?? {
????? this[i]=instance[i];//將父類(lèi)中的元素匹配給她的子類(lèi)
?? }??
}
var c = new child();
alert(c.x);
2.父類(lèi)同上
function child()
{
?? this.parent=parent;
?? this.parent();
?? delete this.parent;
}
var c = new child();
alert(c.x);
3.父類(lèi)同上
這次用js提供的Call方法
functon child()
{
?? parent.call(this);
}
var c = new child();
alert(c.x);
原型如下:
function parent(){
}
parent.prototype.x=1;
function child(){
}
for(var p in parent.prototype)child.prototype[p]=parent.prototype[p];
var c=newchild();
alert(c.x);
?
function parent(string){
??? var child=new Function("this.x=1;"+string);
??? return child;
}
var child=new parent("this.y=2;");
var c=new child();
alert(c.y);
?
function parent(){
??? this.x=1;
}
function child(){
}
child.prototype=new parent();
var c=new child();
alert(c.x);
?
?function parent(){
??? this.x=1;
}
function child(){
??? var ret=new parent();
??? ret.y=2;
??? return ret;
}
var c=new child();
alert(c.x);
本文采編于租賃寶網(wǎng)內(nèi)部技術(shù)人員 參考網(wǎng)址:http://www.zulinbao.com
轉(zhuǎn)載于:https://www.cnblogs.com/systemxgl/archive/2010/10/29/1864498.html
總結(jié)
以上是生活随笔為你收集整理的javascript中的继承方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: spring核心:bean工厂的装配 6
- 下一篇: c#抓取別的網頁的內容