javascript里面的这个伪继承貌似跟java里面的继承有那么点点的相似:
伪继承的5种方式:
/*Shape为被父类,父类里面有属性和方法*/
A,for in 循环
var e={};
var s = new Shape(5,4,"三角形");
for(var i in s){
e[i]=s[i];
}
B,对象冒充
function E(){
this.newMethod=Shape;
this.newMethod(5,4,"三角形");
}
var e = new E();
C,call方法
function E(){
Shape.call(this,5,4,"三角形");
}
var e=new E();
D,apply
function E(){
var array=[5,4,"三角形"];
Shape.apply(this,array);
}
var e = new E();
E,原型链
function E(){}
E.prototype=new Shape(5,4,"三角形");
var e = new E();
注意:宿主对象不能被继承