-2

Why is it possible to reference an object from within the object directly? For example:

var object = {
    prop1 : "Prop 1",
    prop2 : "Prop 2",
    func : _ => console.log(object)
}

object.func();

//output {prop1: 'Prop 1', prop2: 'Prop 2', func: ƒ}

Similarly I could do:

var object = {
    prop1 : "Prop 1",
    prop2 : "Prop 2",
    func : function() { console.log(this) }
}

object.func();

This will result in the same output as above. I am mainly trying to understand why it is legal to reference the variable name object from inside its declaration.

Austin737
  • 675
  • 1
  • 8
  • 15

1 Answers1

2

Attempts to access variables inside functions are not resolved until the function is called … which will be after the object is created.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335