3

I'm reading more open source javascript frameworks and found more ways of how to create anonymous functions, but whats is different and best way?

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

(function() {
    var Lib = {}; window.Lib = Lib;
})();

(function(global) {
    var Lib = {}; global.Lib = Lib;
})(global);
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
kichu
  • 55
  • 5
  • 4
    There is only one way to create an anonymous function, and that is not what you're asking about here. – Matt Ball May 13 '12 at 21:58
  • 3
    Those are _self-executing_ functions. – SLaks May 13 '12 at 21:59
  • best way is what is more readble for u and what is less error prone (here they try not to "polute" the Global NS (window) or try to dynamicly decide what is the scope of the function and then call it only once. I would do it is a straight way, anyway. – Itay Moav -Malimovka May 13 '12 at 22:00
  • 1
    @SLaks a more proper name would be [*immediately invoked function expressions* (IIFE)](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) – Joseph May 13 '12 at 22:06
  • @MattBall there is actually more than one way to create an anonymous function. `new Function()` comes to mind. – bkconrad May 13 '12 at 22:12
  • @bkconrad fair enough. That's analogous to `new Object()` vs `{}`. – Matt Ball May 13 '12 at 22:22
  • There's a slightly more significant difference with `new Function()`: functions created this way always run in the context of `window` rather than the context they're defined in (as `function () {}`s do) – bkconrad May 13 '12 at 22:26

3 Answers3

3
(function() {
    this.Lib = {};
}).call(this);

Defines the Lib property of the Object it's called on, and is immediately called on this which is typically the window. It may instead refer to the Object that owns the method in which it is called.

(function() {
    var Lib = {}; window.Lib = Lib;
})();

Defines the Lib property of window regardless of where it's called (although it's also called immediately).

(function(global) {
    var Lib = {}; global.Lib = Lib;
})(global);

Defines the Lib property of the object passed to the function. It's called immediately, but will result in an error unless you've defined a value for global in the current scope. You might pass window or some namespacing Object to it.


These aren't actually different ways of defining "anonymous functions", they all use the standard way of doing this. These are different methods of assigning values to global (or effectively global) properties. In this sense, they are basically equivalent.

Of more significance would be, for instance, how they define methods and properties of the objects they return/construct/expose (that is, how they build Lib itself).

All of these functions return undefined and only the first could be usefully applied as a constructor (with the use of new), so it would seem they are nothing more than initializers for the frameworks.

bkconrad
  • 2,502
  • 3
  • 17
  • 30
1

All of them are effectively equivalent (but less efficient and more obfuscated) to:

var Lib = {};

Immediately invoked function expressions (IIFEs) are handy for contianing the scope of variables that don't need to be available more widely and conditionally creating objecs and methods, but they can be over used.

Note that in the last example, I think you mean:

(function(global) {
  ...
})(this);
RobG
  • 124,520
  • 28
  • 153
  • 188
0

Check out this question: Why do you need to invoke an anonymous function on the same line?

It contains a whole lot of information about anonymous functions. Check out this question as well: var functionName = function() {} vs function functionName() {}

Community
  • 1
  • 1
ChrisH
  • 1,279
  • 1
  • 9
  • 22
  • While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Matt Ball May 13 '12 at 22:14
  • You're absolutely right Matt, little bit short on time though. It's a great read, but should indeed include a bit more explanation. Will use it in future :) – ChrisH May 13 '12 at 22:18