Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a parameter named "undefined" in a JavaScript function?

While viewing jQuery's uncompressed source code I stumbled upon something I don't quite understand. When the create their anonymous function, they place undefined as a 2nd argument. What is this doing, and why are they using undefined? Is it necessary to put undefined as an argument in an anonymous function? Below is an example of what I am talking about.

(function( window, undefined)  {
  ...code here
})( window );
like image 444
JaPerk14 Avatar asked Jun 23 '12 06:06

JaPerk14


People also ask

What is the use of undefined in JavaScript?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

What's the purpose of a parameter in JavaScript?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions.

Why a variable is undefined?

An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been declared by that code. In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time.

How do you declare an undefined variable in JavaScript?

Note: The undefined is not a reserved keyword in JavaScript, and thus it is possible to declare a variable with the name undefined. So the correct way to test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined') .


2 Answers

What this is doing, is reassigning undefined to undefined inside that closure. Thats a failsafe. Because other code may accidentally do something like

undefined = something;
console.log(undefined); // will output 'something'

And thats valid in javascript(if the JS engine being used has not implemented ECMAScript 5 specification, in ECMAScript 5 spec undefined is non non-writable, MDN DOC ),

Quote from MDN New_in_JavaScript 1.8.5 (ECMA 5) Page

Changes to global objects

Global objects made read only

The NaN, Infinity, and undefined global objects have been made read only, per the ECMAScript 5 specification.

And from ES5 Annotated Spec in Guthub

ES5 spec Section x15.1.1.3

15.1.1.3 undefined

The value of undefined is undefined (see 8.1).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

Even if global undefined is not writable You can have a local variable named undefined and can mess up your code(mainly comparisons with undefined). But that's your responsibility. You can have codes like

(function(){
    console.log('Second Case: ');
    var undefined = 'Something';
    console.log(undefined); // Will log `something`
    var a ; // a is undefined
    console.log(a  ===  undefined); // false, as undefined is changed
    // you might expect a === undefined will return true, but as 
    // `undefined` is changed it will return false.
    console.log(a); // undefined
})();

Demo: http://jsfiddle.net/joycse06/V4DKN/

However if undefined is writeable then the above assignment may hamper many comparison made with undefined after that line of code as undefined is not undefined anymore. It has some value now.

So as they are calling that anonymous function like

( window ) // one argument only

And receiving

( window, undefined)  // only window is passed when calling the function
          // Second argument is not passed means it's undefined
          // so undefined is restored to undefined inside that function
          // and no global accidental assignments can hamper jQuery's 
          // code using 'undefined' now

That means inside that closure undefined is restored to undefined, as it has not been passed any value thus securing use of undefined inside that anonymous function.

A very good detailed article on this http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/

I am quoting some lines from the above article link to make things clear

What is undefined?

In JavaScript there is Undefined (type), undefined (value) and undefined (variable).

Undefined (type) is a built-in JavaScript type.

undefined (value) is a primitive and is the sole value of the Undefined type.

Any property that has not been assigned a value, assumes the undefined value. (ECMA 4.3.9 and 4.3.10).

A function without a return statement, or a function with an empty return statement returns undefined. The value of an unsupplied function argument is undefined.

var a;
typeof a; //"undefined"

window.b;

typeof window.b; //"undefined"



var c = (function() {})();

typeof c; //"undefined"



var d = (function(e) {return e})();

typeof d; //"undefined"

undefined (variable) is a global property whose initial value is undefined (value), Since its a global property we can also access it as a variable. For consistency I’m always going to call it a variable in this article.

typeof undefined; //"undefined"
var f = 2;
f = undefined; //re-assigning to undefined (variable)
typeof f; //"undefined"

As of ECMA 3, its value can be reassigned :

undefined = "washing machine"; //assign a string to undefined (variable)
typeof undefined //"string"
f = undefined;
typeof f; //"string"
f; //"washing machine"

Needless to say, re-assigning values to the undefined variable is very bad practice, and in fact its not allowed by ECMA 5.

like image 55
Prasenjit Kumar Nag Avatar answered Sep 17 '22 14:09

Prasenjit Kumar Nag


Undefined is a type but is also a global variable.

You can have a module that overwrites the value of undefined by doing undefined = whatever.

undefined in the is actually an undefined parameter of a function wrapping the whole code:

(function(window, undefined) {
    // undefined is the undefined parameter
}(window)); 

It's safe, as the undefined parameter is in local scope, and no one except the code in this function can assign to it.

It's not necessary to use undefined as parameter when define an anonymous function.

If you see above function you wll notice that it expects two parameter but one is supplied.

Why undefined need to restored?

because, to makes sure that undefined is indeed undefined in the scope between curly braces, even if someone has written something like undefined = "defined"; in the global scope, because undefined can actually be redefined.

So if you have something like

var undefined = 1;

(function(window, undefined) {
  console.log(undefined); // output will be undefined not 1
}(window));
like image 20
thecodeparadox Avatar answered Sep 19 '22 14:09

thecodeparadox