mechanicious
mechanicious

Reputation: 1616

Placing arguments for anonymous functions in JavaScript

When I make an anonymous function in JavaScript like this:

(function(){
/* some code here */
})()

In which object will be this function added, and where will this function live?

Also you can see in the jQuery source code an anonymous function like this:

(function(window, undefined){
    /* some code here */
    })(window)

How do this function's arguments differentiate it from an anonymous, 0-arg function?

Upvotes: 4

Views: 163

Answers (3)

Pointy
Pointy

Reputation: 414086

Functions in JavaScript are values. That is, a function is represented by an object, and like any other object it can be the value of a variable or participate in expressions.

Thus

(function() { ... })

is a value, just like 17 or "hello world" is a value.

When a function (as a value) appears in an expression, and it's followed by (...) with a comma-separated list of expressions between the parentheses, that's a function call.

OK, so:

(function() { ... })()

creates a function (as a value) and then invokes that function with no arguments. The function object, at least as a direct result of that code, is not stored anywhere. It essentially vanishes after the function call completes, and the overall value of that subexpression will be whatever the function returned.

Passing parameters to such a function is no different than passing parameters to any other function. In the specific example you quote, the purpose is to prevent certain kinds of anomalies caused by errant "alien" code. Your example really should read:

(function(window, undefined) {
  // code
})(this);

The symbol this is a reserved word and its value is under complete control of the runtime. (Well, it's value in a local execution context is thusly controlled.) When evaluated in the global scope, the above code ensures that inside the anonymous function, the symbol "window" will be a reference to the global context. That sort of construct is also useful for code that may be used in contexts other than a browser, like Node.js for example, where the global context isn't called "window".

Upvotes: 4

vdua
vdua

Reputation: 1331

Adding to @Pointy's answer adding parameters to an anonymous function doesn't make any difference

(function(){
  /* some code here */
})()

(function(window,undefined){
  /* some code here */
})(window)

Both the functions are lost after they are called, the only difference is that inside the second function some variables or functions are being stored in window context, but the anonymous function in itself is lost after the call.

If you need to keep the reference to the function try

(window.myFunc = function(arg1, arg2, arg3) {
   /* your code here*/
})(arg1, arg2, arg3)

Upvotes: 0

Matt Fenwick
Matt Fenwick

Reputation: 49115

Both examples you gave are anonymous functions, according to the first line of the Wikipedia definition:

an anonymous function [...] is a function (or a subroutine) defined, and possibly called, without being bound to an identifier

The arguments do not make a difference with respect to anonymity. Anonymous functions can take 0, 1, 2, ... n arguments, just like non-anonymous functions (i.e. named functions).

One major advantage of anonymous functions is that they don't have to live anywhere -- they can be defined and used inline, just like other values of other types.

Upvotes: 1

Related Questions