以下是解释我的疑虑的准备代码,
function Person(name) {
this.name = name;
}
Person.prototype.getname = function(){
return this.name;
}
function test(){}
test.prototype = new Person('why')
var p = new test();
console.log(p.getname()) // the outcome is ofcourse 'why'
p.__proto__.name = 'go'
console.log(p.getname()) // the outcome has been changed to 'go'
这就是问题所在。
test.prototype = new Person('goo')
console.log(p.getname()) // the outcome is still 'go' not 'goo'
为什么结果仍然“去”不是'goo'?
之间有什么区别
p.__proto__.name = 'go' vs test.prototype = new Person('goo')?
猜测我现在误解了内部操作系统。
需要一些帮助,