0

In following code of jQuery, why next parameter is undefined

JavaScript:

(function(a,b){
    ....
})(window)

Here a=window but b=undefined, why is so?

blue ghhgdtt
  • 907
  • 4
  • 11
  • 16
  • That's not jQuery. That's JavaScript. jQuery is a library of JavaScript functions (none of which you're using there). The *language* it uses, and which you're using there, is JavaScript. – T.J. Crowder Mar 01 '13 at 08:14
  • @T.J.Crowder that's true, however this actually IS the jquery code. Though I agree with you, too many people confuse jquery and javascript, same problem sadly with JSON. – Christoph Mar 01 '13 at 08:27
  • @Christoph: Right. Which is the point of my explaining it to the OP. – T.J. Crowder Mar 01 '13 at 08:30
  • Didn't you mean to ask [this question](http://stackoverflow.com/q/2716069/165154), where the second parameter's name literally is `undefined`? – Decent Dabbler Mar 01 '13 at 09:15

3 Answers3

7

That's a common technique to assure you have a true undefined value to check against since the undefined property of the window object used to be writeable and thus could not reliably be used in checks. Since only one parameter is handed over to the function, the second one is assured to be undefined. The name of that variable does not matter, it could be undefined but as well foobar or, as in this case, (because this is the shortest possible way saving valuable bytes) b.

Now you can safely check for undefinedness of variables, because you are sure about the value of b:

// overwriting the global undefined property works in old browsers
undefined = 3;

(function(a,b){

    var asd; // not initialised, thus undefined

    if (asd === undefined){
       // should trigger, but won't because undefined has been overwritten to 3
    }
    if (asd === b){
       // is safe, bcause you know that b is undefined
    }

})(window)

New browsers (IE9, FF4+, Chrome) obey the EcmaScript5 specification and undefined is not writable any more.

Christoph
  • 50,121
  • 21
  • 99
  • 128
6

Because you are not passing anything for the second parameter. You are passing just one, namely window.

Your code

(function(a,b){
    ....
})(window)

defines a function and calls it immediately. The last line above actually calls the function using the window parameter. If you pass 2 parameters there, b will not be undefined.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • What that means "you are not passing anything for the second parameter"?, i didnt understand. If second parameter is nothing to pass, then is it necessary to keep it undefined? – blue ghhgdtt Mar 01 '13 at 08:15
  • @blueghhgdtt see my answer for explanation to that question. – Christoph Mar 01 '13 at 08:17
  • When you pass nothing, its value will be `undefined` (or does not exist). Checking if an argument is undefined is actually a pretty common technique to see if a parameter has been passed in or not. – techfoobar Mar 01 '13 at 08:18
  • @blueghhgdtt: You "pass" arguments into a function. So in `foo("bar");`, we're "passing" the value `"bar"` into the function `foo`. Your anonymous function accepts two arguments, `a` and `b`. But the call to it (the bit in parens at the end) is only passing *one* argument to it. That argument ends up coming into the function as `a`. Since there is no second argument passed into the function (it's just `...(window)`, not `...(window, somethingElse)`), `b` keeps its default value of `undefined`. – T.J. Crowder Mar 01 '13 at 08:22
  • since we just want to pass 'window' only and we never have 'somethingElse' to pass then is it necessary to keep 'b' 'undefined' – blue ghhgdtt Mar 01 '13 at 08:26
  • Can anyone explain me about this technique or provide me reference. – blue ghhgdtt Mar 01 '13 at 08:27
  • @blueghhgdtt: Any half-decent JavaScript tutorial will talk about the fact that if you have a function that declares two arguments (`function foo(a, b)`) and then you call it with only one (`foo("bar")`), `b` will be `undefined` in the call. – T.J. Crowder Mar 01 '13 at 08:32
  • @TJCrowder, i know what you just said, but what i want to know is, if we have only one parameter to pass then why should we create function with two arguments? – blue ghhgdtt Mar 01 '13 at 08:37
  • @blueghhgdtt i explained it in my [answer](http://stackoverflow.com/a/15153248/1047823) why this is done. If you have any further questions feel free to ask – Christoph Mar 01 '13 at 09:14
1

This is immediate javascript function syntax you're trying to use:

(/* function body */)(/* params */)

Named function defined like:

function addNumbers(a, b){
    alert(a + b);
}

You call it:

addNumbers(1, 2)

Or similar immediate function (defined and executed at the same time):

(function(a, b){
    alert(a + b);
})(1, 2)
Nenad
  • 24,809
  • 11
  • 75
  • 93
  • If we don't have second parameter `2` to pass then is it necessary to keep `b` as an argument? – blue ghhgdtt Mar 01 '13 at 08:56
  • Then just make (function(a){ /* code */})(1). – Nenad Mar 01 '13 at 19:22
  • yeah, that's right, then why javascript code in jquery pass `window` only but `function` arguments are `a`&`b`. Why, there is not only `a`? – blue ghhgdtt Mar 01 '13 at 19:26
  • Yes, that's also valid. "b" can be used as optional parameter. In that case you have to check if b is undefined inside of the function. Example above would be: "if (b === undefined){ alert(a) } else { alert(a + b); }" – Nenad Mar 01 '13 at 19:31
  • What's the benefit of checking `b` , `undefined` or not! since `b` is always `undefined` and we are passing just `window` parameter, which is equal to `a` and `b` is always `=` to `undefined`. Why javascript in jquery passed `undefined` paramter to argument `b`? what's the reason or its advantages... – blue ghhgdtt Mar 01 '13 at 19:37
  • Ah, your question was not clear. It's specific to jQuery initialization? Then this is duplicate question! Read answer to original question. It's linked now to your post. – Nenad Mar 01 '13 at 20:54