2

Possible Duplicate:
jQuery question: what does it really mean?

For example,

(function (exports, $, undefined) {
// Code goes here
}(window.Chat = {}, jQuery));

What is the point of passing in "undefined" especially when there isn't a third parameter? I've seen this syntax in multiple places.

Community
  • 1
  • 1
Zack Burt
  • 8,257
  • 10
  • 53
  • 81
  • This is absurd IMO. It's like cooking, while placing a potato on the table during cooking, and then returning that potato to the refrigerator (not using it). – Saeed Neamati Nov 22 '11 at 06:12
  • 1
    possible duplicate of [jQuery question: what does it really mean?](http://stackoverflow.com/questions/5305634/jquery-question-what-does-it-really-mean) oops, that post was itself a duplicate of http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined – Yuji 'Tomita' Tomita Nov 22 '11 at 06:15
  • 2
    Apparently, it's used to ensure that `undefined` is actually undefined, and not overridden in the outer scope by some clever coder. – Yuji 'Tomita' Tomita Nov 22 '11 at 06:16
  • This question has been asked approx. 8,000 times.. And it always gets upvoted.. Next time I need some points I think I'll ask this question.. – Mike Christensen Nov 22 '11 at 06:27

2 Answers2

1

"Undefined", for some reason, isn't a protected keyword in JavaScript. If you try to compare something to undefined you have no guarantee you're not comparing to some variable named undefined. The code you refer to is a trick to work around this stupid issue. The parameter is guaranteed to actually be undefined since it's an immediately invoked function where the writer of the code knew they weren't passing the third parameter, so it will be the real undefined.

0

undefined value can be changed, passing it to the function without assigning a value make it safe

Flatlineato
  • 1,066
  • 15
  • 32