0

myTable.div will return "div_undefined". When I'm changing div: 'div_' + this.name to div: 'div_' + myTable.name it will throw an error:

Uncaught TypeError: Cannot read property 'name' of undefined at :3:26

. Any Ideas?

var myTable = {
   name: 'tablename',
   div: 'div_' + this.name
}
console.log(myTable.div);
dwing265
  • 120
  • 1
  • 13

1 Answers1

1

You can first declare your object then add the div property:

myTable.div = 'div_' + myTable.name;

Keep in mind that objects can be always extended with more properties, if you check the MDN Object reference you can see that:

In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed.

Demo:

var myTable = {
  name: 'tablename'
};
myTable.div = 'div_' + myTable.name;
console.log(myTable);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78