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.
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.
this is a keyword in JavaScript that is a property of an execution context. Its main use is in functions and constructors.
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.
“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 .
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"
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.
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 } ());
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.
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();
})();
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With