constructor 指针丢失 详解
function User(name) {
this.name = name
}
//添加多个方法时
User.prototype = { // 等于新开辟了个内存地址 如果不把constructor 添加 会导致constructor指针丢失
constructor: User, // constructor 丢失 //解决方法 **** 注意
show() {
console.log(this.name)
}
}
User.prototype.show = function () { //没问题 添加多个方法 太麻烦
console.log(this.name)
}
let lisi = new User.prototype.constructor('李四')
lisi.show()

浙公网安备 33010602011771号