面向對象-原型的繼承
阿新 • • 發佈:2018-09-08
面向 console pre const 新建 true his 一個 ply
//父構造函數 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();
面向對象-原型的繼承