简体   繁体   English

为什么两个函数调用都返回相同的值?

[英]Why both the function calls return the same value?

See the code segment below: 请参阅以下代码段:

var o = {f:function(){ return this.a + this.b; }};
var p = Object.create(o);
o.a = 10;
o.b = 20;

console.log(o.f());  // output: 30
console.log(p.f());  // output: 30

The object p doesn't have property pa and pb then how pf() return output 30. Is that prototype chain? 对象p没有属性papb,那么pf()如何返回输出30.是原型链吗? Could anyone explain this? 有人能解释一下吗? Thanks in advance. 提前致谢。

Here o is the prototype of the p object, so all the propeties of o is available in p . 这里op对象的原型 ,所以o所有属性都可以在p

So when you call pf() , you will get the values assigned to o in this.a and this.b 因此,当您调用pf() ,您将获得在this.bthis.b分配给othis.a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM