继承-原型链继承

mac2026-06-15  16

// 原型链继承:

// 既能继承原型身上的属性和方法,又能继承构造函数中的this的属性和方法 // 不方便传参 function Parent(n){ this.num = n; } Parent.prototype.init = function(){ console.log("hello") } function Child(n){} Child.prototype = new Parent(520); Child.prototype.init = function(){ console.log("html") } var p = new Parent(3.1415); p.init() console.log(p.num) var c = new Child(); c.init() console.log(c.num) console.log(p); console.log(c);
最新回复(0)