Javascript: undefined as function parameter

This page displays sample code containing the following line:

var Subject = ( function( window, undefined ) {

What is a undefinedfunction parameter?

+5
source share
4 answers

This is used to prevent overriding the value undefinedin non-strict mode.

In lax mode, the value undefinedcan be overridden by assigning it a different value.

undefined = true; // Or any other value

So, using a value undefinedwill not work properly.

In strict mode, it undefinedis read-only, and assigning it a value will cause an error.

, undefined.

var Subject = ( function( window, undefined ) {

}(window)); // <-- No parameter is passed for the last value
+6

, undefined undefined. JavaScript, undefined , , , :

 undefined = 2; // Assign a different value to undefined
 if (undefined == 2)  // Now this statement would be true

,

var Subject = ( function( window, undefined ) {

, undefined, undefined undefined.

+3

undefined, . Undefined - , . true, :

if(my_var === undefined) { 
     dont_load_missiles());
} else {
     load_missiles();
}
0

(SO link), .

() ;

  • undefined .
  • undefined , .

,

var undefined = 7;
function printUndefined(undefined) {
  document.getElementById("output").innerHTML += undefined + "<br/>";
}

printUndefined(); // Output is undefined
printUndefined(undefined); // Output is 7
printUndefined(10); // Output is 10

JSfiddle

, .

0

Source: https://habr.com/ru/post/1613433/


All Articles