4

Possible Duplicate:
JavaScript function with ‘undefined’ parameter

I'm looking the jQuery Color source code here

http://code.jquery.com/color/jquery.color-2.1.0.js

And I found that the closure function take an undefined value as it's second parameter. See below:

(function( jQuery, undefined ) {

    var stepHooks = "backgroundColor borderBottomColor borderLeftColor borderRightColor borderTopColor color columnRuleColor outlineColor textDecorationColor textEmphasisColor",

    // plusequals test for += 100 -= 100
    rplusequals = /^([\-+])=\s*(\d+\.?\d*)/,
    // a set of RE's that can match strings and generate color tuples.

Or you can see it in the source code. Look at the second parameter.

The point I want to know is that why the second parameter is undefined?

I think it is an approach to strictly set the function to receive only one parameter.

I'm I right? Or anyone can help me out?

Community
  • 1
  • 1
Xieranmaya
  • 792
  • 10
  • 11

1 Answers1

5

That's in case some other part of the code assigns some value to the undefined name. The closure is actually called with only one argument, as:

(function(jQuery, undefined) {
    // ...
})(jQuery);

That ensures that undefined is actually bound to undefined within the closure.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 4
    Because, for some unfathomable reason, `undefined` is not a keyword. – Niet the Dark Absol Oct 26 '12 at 17:36
  • still dont get it.. so is it just related to naming convention? any example?? – Amitd Oct 26 '12 at 17:39
  • 1
    @Amitd, let's consider that a (possibly malicious) script in the page assigns `"foo"` to `undefined`. Without this workaround, comparisons to `undefined` would not work properly (they would become comparisons to `"foo"`) and the resulting behavior would be quite hard to understand and diagnose. – Frédéric Hamidi Oct 26 '12 at 17:41
  • That is, "undefined" is't a keyword and can assign a value to it? Let me try it... – Xieranmaya Oct 26 '12 at 17:47
  • I've tried, but I can't assign a value to undefined. It came back to itself after the assignment. In Chrome – Xieranmaya Oct 26 '12 at 17:49
  • 1
    @Xieranmaya, you must be using a recent browser that conforms to version 5 of the ECMA specs. Not all browsers do that. – Frédéric Hamidi Oct 26 '12 at 18:04