您的位置:首页 > Web前端 > JavaScript

javascript中的继承方式

2010-10-29 15:12 344 查看
javascript中的继承方式有好几种。

下面分别举例供大家参考学习:

1.function parent()

{

this.x=1;
}

function child()

{

var instance=new parent();//实例化父类

for(var i in instance)

{

this[i]=instance[i];//将父类中的元素匹配给她的子类
}
}

var c = new child();

alert(c.x);

2.父类同上

function child()

{

this.parent=parent;

this.parent();

delete this.parent;

}

var c = new child();

alert(c.x);

3.父类同上

这次用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);

本文采编于租赁宝网内部技术人员 参考网址:http://www.zulinbao.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: