一般继承方式如下
function people(n){
this.name =n;
}
function worker(name,num){
people.call(this, name);
this.num =num;
}
worker.prototype =new people()
worker.prototype.constructor = worker
1 原型方式比较灵活
function people(n){
this.name =n;
}
可以先生成对象,然后添加原型方法
var p1 = new people('zj')
people.prototype.show = function(){
console.log(this.name)
}
p1.show()//正确
但是原型重写就有可能有问题
function people(n){
this.name =n;
}
var p1 = new people('zj')
people.prototype ={
show:function(){
console.log(this.name)
}
}
var p2 = new people('zj2')
p1.show()//error,没有这个方法,因为prototype被覆盖了
p2.show()//正确