javascript中的继承方式

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

原文地址:https://www.cnblogs.com/systemxgl/p/1864498.html