I am trying to understand JavaScript this better.
function foo() {
console.log(this);
}
// normal function call
foo(); // `this` will refer to `window`
When I try to pass this to a function it throws an error.
function foo(this) {
console.log(this);
}
foo(); //Uncaught SyntaxError: Unexpected token this(…) on chrome console.
When I try to pass a variable that points to window I get undefined.
var that = this;
function foo(that) {
console.log(that):
}
foo(); // undefined chrome console.
I was expecting window object in last example as when I type that on console I get window object.
>>> that
window....