0

I read this once in a script, but I didn't understand its benefits.

(function () {}).call(this); 

Also, I tried running it,

(function () { console.log('Hello World') }).call(this); 

but it didn't print anything.

Even calling this (function () { console.log('Hello World') })() didn't print anything either. Whats wrong?

Naughty.Coder
  • 3,922
  • 7
  • 32
  • 41
  • It will print Hello World when you run it , just tried it on firebug console . – Tarek Feb 12 '14 at 18:06
  • I ran it on the console .. but nothing got printed. – Naughty.Coder Feb 12 '14 at 18:06
  • http://jsfiddle.net/dublinan/QvJJt/ For example, if you open this page in google chrome, then right click anywhere on the page and inspect element -> console, u can see the output. – MiKE Feb 12 '14 at 18:07
  • 1
    That is sane code. If you run it in your browser's console, it will print Hello World. – T0xicCode Feb 12 '14 at 18:07
  • Works fine for me. It's a [Self-Executing Anonymous Function][1] [1]: http://stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript – Dave Ross Feb 12 '14 at 18:08

2 Answers2

2

I use this when I want my code to work in either a browser window (in which case this refers to window) or a web worker (in which case this refers to the worker instance).

The .call(this) works well for this, though of course you could do it differently. For example, this would also work:

(function(root) {

    root.MyLibrary = MyLibrary;

}(this));

As always, there's more than one way to skin the proverbial cat.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
0

Probably the writer wants to change the context of the IIFE. Passing this would be pointless, since it will refer to window even without the call.

Something like this would change the meaning of this in the scope:

(function () { console.log('Hello World', this) }).call({foo:1});

However, I still don't see the point, since you might as well do it like this:

(function (fooObj) { console.log('Hello World', fooObj) })({foo:1});

Which I would say is more common to see.

Johan
  • 35,120
  • 54
  • 178
  • 293
  • You're assuming it's executed in the global environment. – cookie monster Feb 12 '14 at 18:10
  • @cookiemonster Well, without any better explanation from OP, one can only assume that it's the case – Johan Feb 12 '14 at 18:11
  • Why? There's no need to make such assumptions. As for the second part of your answer, consider that JS is getting new function syntax which has a lexically defined `this`, which would make the enclosing `this` value potentially important. – cookie monster Feb 12 '14 at 18:13
  • @cookiemonster Regardless, you could still pass `this` as an argument to the IIFE. Using `call` to invoke something which is already self-invoking is wrong. – Johan Feb 12 '14 at 18:15
  • It's very far from being "wrong". And with the new function syntax, it would be necessary if you want the functions to have the enclosing `this` value. Passing as an argument wouldn't help. – cookie monster Feb 12 '14 at 18:20
  • @cookiemonster If you insist on using the `this` keyword instead of an argument - then sure. Functionality-wise = no difference. – Johan Feb 12 '14 at 18:23
  • `this` to me means the `context` and so can be descriptively useful when compared to a standard variable `foo`, which would not necessarily denote itself as the `context`, even if there is functionally no difference. – Xotic750 Feb 12 '14 at 18:36