this javascript 1

this javascript

// this = current execution context (window in browser, global in nodejs)
console.log(this) // window object

function foo () {
 console.log(this); // object calling this function

foo(); // undefined

o={ foo }; // 'o' object logged

Here is what the above code is Doing:
1. The first console.log(this) logs the window object.
2. The second console.log(this) logs undefined.
3. The third console.log(this) logs the object that called the function.

Similar Posts