Heim >Web-Frontend >js-Tutorial >javascrip关于继承的小例子_javascript技巧
//Worker.prototype=Person.prototype;//如果这样负值原型,子类的sayJob方法Person父类也会有sayJob方法,因为是引用传递
//改成如下方式则子类不会影响父类:
for(var i in Person.prototype)
{
Worker.prototype[i]=Person.prototype[i];
}
Worker.prototype.sayJob=function()
{
alert(this.job);
}
var p=new Person('lisi','男');
//alert(p.sayJob);
var w=new Worker('zhangsan','男','打酱油的');
w.sayName();
w.saySex();
w.sayJob();