Skip to main content
Tweeted twitter.com/StackSoftEng/status/1381396696043827200
Question Protected by gnat
edited title
Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

Is this`this` in JavaScript an example of dynamic scoping?

Source Link

Is this in JavaScript an example of dynamic scoping?

Variables in JavaScript are lexically scoped. But, I wonder, is the this keyword, referring to the receiver of a method, an example of dynamic scoping. Or is this unrelated to the lexical/dynamic scoping discussion?

var foo = { x = “how am I scoped?” }
function bar() {
    console.log(this.x) // the free variable this is decided by the calling context, like I see dynamic scoping described
}
foo.bar()