问题
As you see,undefined is not a keyword in javascript(Is it right?).So we can use it as a variable though I'm sure this is a bad idea.Now my question is why I can't give a value to undefined.
Code:
var undefined=3;
undefined;//still undefined
Any suggestions will be thankfull.
回答1:
Because in older environments(before JS 1.8.5) it was possible to change the value of undefined, and checking for something that didn't exist was made in a way that was possible to break. It is a common choice to develop libraries inside their own closures, using the IIFE pattern to create a variable undefined which is not touched from external code(jQuery for example, and most libraries), this is an example:
(function(undefined) {
//code which safely uses undefined
})();
Note that there is a parameter called undefined
and the IIFE is invoked without any parameter, making that undefined
effectively become undefined
. In newer environments it is not possible to change the value of undefined, but for retrocompatibility it is yet used a lot.
Yet the safest way to check for an undefined type is to use typeof
. Its usage, in fact, cannot be overridden in any way, and it can be used safely: typeof foo == 'undefined'
.
Remember that there is a slight difference between undefined and undeclared:
var x; //declared but undefined
x === undefined; //true
But in older environments you were able to do
var undefined = 1;
var x; //declared, of type undefined, but...
x === undefined // false, because undefined is 1 and x is of type undefined
As I said before, the safest way to check for an undefined variable(being sure it will work in older environments) is to use typeof:
var undefined = 1;
var x;
typeof x == 'undefined'; //true
Remember also that using equality check(==
) with undefined
also checks for null
(in fact, undefined == null
but undefined !== null
), so I recommend using strictly equality operator(===
), which checks only for undefined
.
回答2:
According to the MDN, undefined became non-writable, starting in JavaScript 1.8.5.
That yields that it was fully "legal" to use it as a variable until then.
But it obviously isn't a good idea.
回答3:
See a full explanation (I'm not the author) of how 'undefined' is used in JavaScript here http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/
Compared to other languages, JavaScript’s concept of undefined is a little confusing. In particular, trying to understand ReferenceErrors (“x is not defined”) and how best to code against them can be frustrating.
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.
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.
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 (though amongst the current set of full browser releases, only Safari enforces this).
来源:https://stackoverflow.com/questions/19000087/want-to-know-more-about-undefined