zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JavaScript继承方式实例

2023-06-13 09:14:25 时间

复制代码代码如下:


functionparent(){
this.x=10;
}
functionchild(){
varparentObj=newparent();
for(varpinparentObj)this[p]=parentObj[p];
}
varchildObj=newchild();
alert(childObj.x);


复制代码代码如下:

functionparent(){
this.x=10;
}
functionchild(){
this.parent=parent;
this.parent();
deletethis.parent;
}
varchildObj=newchild();
alert(childObj.x);

复制代码代码如下:
functionparent(){
this.x=10;
}
functionchild(){
parent.call(this);
}
varchildObj=newchild();
alert(childObj.x);


原型抄写
复制代码代码如下:
functionparent(){
}
parent.prototype.x=1;

functionchild(){
}
for(varpinparent.prototype)child.prototype[p]=parent.prototype[p];
child.prototype.y=2;

varchildObj=newchild();
alert(childObj.x);

复制代码代码如下:
functionparent(string){
varchild=newFunction("this.x=10;"+string);
returnchild;
}
varchild=newparent("this.y=20;");

varchildObj=newchild();
alert(childObj.y);

复制代码代码如下:
functionparent(){
this.x=10;
}
functionchild(){
}
child.prototype=newparent();
varchildObj=newchild();
alert(childObj.x);

复制代码代码如下:
functionparent(){
this.x=10;
}
functionchild(){
varret=newparent();
ret.y=20;
returnret;
}

varchildObj=newchild();
alert(childObj.x);