0

I'm learning javascript and I can't figure out why this is not working?

var obj = {
   a : 10,
   b : this.a
}

The 'b' property is undefined. How can I solve this problem?

zolock
  • 1
  • 2
    http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal – ashley Mar 31 '15 at 08:07
  • here's a fiddle - http://jsfiddle.net/4e39sufp/ – Harko Mar 31 '15 at 08:11
  • Because you can't reference an object literal until it is created, one way to solve your problem is to use a variable: `var c = 10; var obj = { a: c, b: c };` or to create and then add properties: `var obj = {}; obj.a = obj.b = 10;` – Pebbl Mar 31 '15 at 08:15
  • Thank you! Much more clear now! – zolock Mar 31 '15 at 08:22

0 Answers0