0

I have a object that basically has a list of toys in it. According to w3schools.com, this keyword in a object refers to the object itself. But the following code returns undefined.

var toy = {
a:'car',
b:'bus',
c:'truck',
d:'desk',
e: this.d
};
console.log(toy.e);

Is the same thing as saying

e:toy.d 
as
e:this.d?

Since this keyword refers to the toy object.

  • 1
    No idea what "w3schools" is, but this information is incorrect. `this` doesn't refer to an object it is "in", `this` is roughly "whoever the current function belongs to". – georg Apr 27 '21 at 22:19
  • @georg, Can you explain your comment better? I am a new learner. Thanks –  Apr 27 '21 at 22:21
  • There' s a pretty decent explanation on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this. For the future, I'd recommend adding "mdn" to all your javascript-related googling, e.g. https://www.google.com/search?q=mdn+promise, https://www.google.com/search?q=mdn+push etc. – georg Apr 27 '21 at 22:25

0 Answers0