Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'this' keyword context inside of a IIFE

Tags:

javascript

function foo() {
  console.log(this.a);
}

var a = 2;

(function() {
  "use strict";

  foo(); // 2
})();

I was just wondering, how come calling foo() still gives the value 2? I thought that since foo is called inside of an IIFE then this.a would evaluate to undefined, since there is no a variable in the IIFE.

like image 372
Mark Avatar asked Feb 07 '21 07:02

Mark


People also ask

How does the this keyword work?

In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

What is this keyword in JavaScript stack overflow?

this is a keyword in JavaScript that is a property of an execution context. Its main use is in functions and constructors.

What is scope of this keyword?

If a function which includes 'this' keyword, is called from the global scope then this will point to the window object. Learn about global and local scope here. In the above example, a function WhoIsThis() is being called from the global scope. The global scope means in the context of window object.

What is this keyword in node JS?

“this” Refers to a Global Object The window object is the global object in the case of the browser. And in a NodeJS environment, a special object called global will be the value of this .

What is IIFE in JavaScript?

IIFE has been used since long by JavaScript community but it had misleading term "self-executing anonymous function". Ben Alman gave it appropriate name "Immediately Invoked Function Expression"

What is the syntax of IIFEs?

Syntax: IIFEs follows a particular syntax as shown below. (function () { // Function Logic Here. }) (); Now let us probe some more to understand the relation between the Name and Syntax of this type of Functions. Why the name Immediately Invoked Function Expressions? Immediately Invoked: This part is easy to explain and demonstrate.

How to use arrow functions to define IIFEs?

Note that you can use an arrow function to define an IIFE: ( () => { //... }) (); By placing functions and variables inside an immediately invoked function expression, you can avoid polluting them to the global object: ( function() { var counter = 0 ; function add(a, b) { return a + b; } console .log (add ( 10, 20 )); // 30 } ());

What is immediately invoked function expression (IIFE)?

An IIFE ( Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. Since our application may incorporate a large number of functions and global variables from various source files, it's critical to keep the number of global variables to a minimum.


Video Answer


2 Answers

The "use strict" is being applied to the IIFE, not the foo() function. As a result, foo gets ran in sloppy mode/non-strict mode. Since foo() doesn't get an explicit this bound to it, it defaults to the global object, which in browsers is the window. When you declare var a in the global scope, it gets added to the window object as a property, meaning using this.a inside of foo will give you the value held in a, as it's accessing the a property of the window.

You would get undefined for this if foo() was being run in strict mode, not the IIFE:

function foo() {
  "use strict";
  console.log(this); // undefined
  console.log(this.a); // Crash
}

var a = 2;

(function() {
  foo(); 
})();
like image 89
Nick Parsons Avatar answered Nov 04 '22 20:11

Nick Parsons


Let's see two things here :-

Firstly your strict mode can apply to globalThis when it's declared in that scope, like so :-

"use strict";

function foo() {
  console.log(this.a);
}

var a = 2;

(function() {
  foo(); // Throws error since this is undefined
})();

Another way could be how @Nick proposed it to run only foo in strict mode.

Now secondly,

The this inside your IIFE is your globalThis which is window for browsers. Variables declared with var in global scope attach themselves to window.

like image 38
Lakshya Thakur Avatar answered Nov 04 '22 18:11

Lakshya Thakur