继承-原型继承

mac2026-06-18  9

// 原型对象继承: // 只能继承原型身上的属性和方法,不能继承构造函数中this的属性和方法

function Parent(n){ this.num = 123; } Parent.prototype.init = function(){ console.log("hello") } function Child(n){} // 原型是不是一个对象 // 直接将原型作为一个对象,复制 // 注意:对象的深浅拷贝 // Child.prototype = Parent.prototype; for(var i in Parent.prototype){ Child.prototype[i] = Parent.prototype[i]; } Child.prototype.init = function(){ console.log("html") } var p = new Parent(3.1415); p.init() console.log(p.num) var c = new Child(520); c.init() console.log(c.num)
最新回复(0)