0

下面是解释我的担忧的准备代码,

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'

为什么结果还是“go”而不是“goo”?

有什么区别吗

p.__proto__.name = 'go' vs test.prototype = new Person('goo')?

我猜我现在误解了内部操作系统。

需要一些帮助,

4

0 回答 0