- 实例化对象的属性继承
function Other(house) {
this.house = house
}
function My(house) {
Other.call(this, house)
}
const mine = new My('我的房子')
- 实例化对象的方法继承
更改原型的指向为要继承的构造函数的实例
更改原型对象的constructor为自己的构造函数
function Other(house) {
this.house = house
}
Other.prototype.sing = function () {
console.log('sing');
}
function My(house) {
}
// console.log(My.prototype == mine.__proto__);
// My.prototype本质是一个空对象.__proto__
My.prototype = new Other()//更改原型对象的指向
My.prototype.constructor = My//构造函数原型对象的constructor构造函数重新赋值
My.prototype.song = function () {
console.log('song');
}
const mine = new My()
const other = new Other('他的房子')
mine.sing()
mine.song()
// other.song()//报错