C
C
celen2012-10-11 17:04:04
JavaScript
celen, 2012-10-11 17:04:04

Why do you need undefined at the beginning of jquery code?

I started learning JavaScript using the jQuery library as an example. A question arose immediately.

The whole library is in an anonymous function, like this

(function( window, undefined ) { /*тут много тысяч строк кода библиотеки*/ })( window );<br>


Why is it necessary to pass undefined as an argument to this function?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Ayzor, 2012-10-11
@celen

undefined is just like any other variable and can be redefined. And this technique allows you to correctly initialize it in the scope of this function

D
Denis Pushkarev, 2012-10-11
@rock

Well, plus so up to 1 character shrinks, if you press.

E
Eugene, 2012-10-11
@Methos

This declares an undefined variable, which has nothing to do with "typeof variable == 'undefined'), other than similar sound and name.
This variable will be undefined. And it can be used to check that another variable is not defined, only with a triple equal sign:
var t;
This:
if (t === undefined) {
}
will have the same effect as
if (typeof t == 'undefined') {
}
But shorter.

E
egorinsk, 2012-10-11
@egorinsk

I have only one question, are there really those who override undefined? Something like paranoia.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question