As we know, JavaScript is a Lexical Scope language:
var a = 1;
function foo() {
console.log(a);
}
function bar() {
var a = 2;
foo();
}
bar() // 1
But when it comes to this
, javascript show Dynamic Scope feature with this keyword.
For example:
var obj = {
foo: function(){
console.log(this)
}
}
var bar = obj.foo
obj.foo() // obj
bar() // window
What is their relationship on earth?