MichaelYu
MichaelYu

Reputation: 675

JavaScript immediately invoked function pattern difference

(function() {
	console.log('immediately invoked function...');
}.call(this));

(function() {
	console.log('immediately invoked function...');
}());

What is the difference between those 2 IIFE patterns? 

Upvotes: 1

Views: 36

Answers (2)

Boldewyn
Boldewyn

Reputation: 82734

The difference is, that in the .call() case, the value of this inside the IIFE is explicitly set (by .call()'s first argument). In the second case it is determined from the way the function is called.

Incidentally, in this case both are the same.

Upvotes: 3

Donald Supertramp
Donald Supertramp

Reputation: 74

In the upper example, the functions context (e.g the this keyword) is set to the context that applies where the IIFE lives)

See Function.prototype.call()

Upvotes: 0

Related Questions