标签:解决 console ons pre argument struct parse 原型 子类
//父构造函数 function Parsent(name.age){ this.name = name; this.age = ahe; } Parsent.prototype.say = function(){ console.log("大家好,我是"+this.name); }; function Child(name,age,job){ Parsent.apply(this,arguments); this.job = job; } //这种方法不可取,因为这是传址,子父类会互相影响 // Child.prototype = Parsent.prototype; //下面的方法可以解决继承过程中子类与父类方法会互相影响的问题 function Temp(){} //新建一个空的构造函数 Temp.prototype = Parsent.prototype; Child.prototype = new Temp(); Child.prototype.constructor = Child; Child.prototype.eat = function(){ console.log("我要吃饭") } var child = new Child("zs",13,"student"); console.log(child.name,child.age,child.job); Child.prototype.say = function(){ console.log("我是child"); } child.say(); child.eat(); var parsent = new Parsent("kt",20); parsent.say();
标签:解决 console ons pre argument struct parse 原型 子类
原文地址:https://www.cnblogs.com/www-dw-com/p/9608258.html