Why do both 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 p object does not have the properties pa and pb, then how pf () returns the result 30. Is this prototype chain? Can anyone explain this? Thank you in advance.

+4
source share
1 answer

Here ois the prototype of the object p, so all propositions oare available in p.

So when you call p.f(), you get the values ​​assigned oto this.aandthis.b

+9
source

Source: https://habr.com/ru/post/1611543/


All Articles