Fraser
Fraser

Reputation: 1521

Accessing outer variable

Object({
  a: "string",
  b: function() { return a; }
}).b()

throws a is not defined. Is it possible to access a from inside b?

Upvotes: 0

Views: 63

Answers (1)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123428

Use this to properly refer the scope

Object({
  a: "string",
  b: function() { return this.a; }
}).b(); // return "string"

See The this keyword on MDN for further reading.

Upvotes: 6

Related Questions