We usually use undefined \color{#FF3030}{undefined}undefined只是 w i n d o w \color{#FF3030}{window} An attribute under the w i n d o w object.
Object.getOwnPropertyDescriptor(window, undefined);
//{value: undefined, writable: false, enumerable: false, configurable: false}
It is precisely because of this that it can be modified in the function scope.
But the prerequisite is to add a statement, because without a statement, the operating window. undefined \color{#FF3030}{window.undefined}window.undefined,而 w i n d o w . u n d e f i n e d \color{#FF3030}{window.undefined} The attribute descriptor of w i n d o w . u n d e f i n e d is:
Attributes | value |
---|---|
value | undefined |
writable | false |
enumerable | false |
configurable | false |
v a l u e \color{#FF3030}{value} v a l u e cannot be modified, cannot be traversed, andattribute descriptor\color{#FF3030}{attribute descriptor}Genus properties described above character can not be modified (not byO bject. Define P roperty \ colorObject.defineProperty来修改 w r i t a b l e 、 e n u m e r a b l e 、 c o n f i g u r a b l e \color{#FF3030}{writable、enumerable、configurable} writable、enumerable、configurable的值)。
But we can do it in the function scope\color{#FF3030}{function scope}Function number for a domain or ablock-level scope \ color {# FF3030} {} block-level scopeBlock level for a domain ofundefined \ color {# FF3030} {"Modify"the value of u n d e f i n e d \color{#FF3030}{"Modify"}" Repair change " , such as:
function fn(){
undefined = 1111;
console.log('undefined : ' + undefined);
}
fn();//undefined : undefined
function fn(){
var undefined = 1111;
console.log('undefined : ' + undefined);
}
fn();//undefined : 1111
{
let undefined = 1111;
console.log('undefined : ' + undefined);//undefined : 1111
}
This is because in the function scope\color{#FF3030}{function scope}Function number for a domain or ablock-level scope \ color {# FF3030} {} block-level scopeBlock level for a domain is also defined in aundefined \ color {# FF3030} {u n d e f i n e d variables. In this scope, newly defined variables will be used first instead ofwindow. undefined \color{#FF3030}{window.undefined}w i n d o w . u n d e f i n e d , thus creating a kind ofwindow. undefined \color{#FF3030}{window.undefined}The illusion that the value of w i n d o w . u n d e f i n e d has changed.
So when we judge the type of variable, it is best to use:
var obj;
typeof obj === 'undefined';
Don't use:
var obj;
obj === undefined;
Use the void keyword\color{#FF3030}{void keyword}V O I D OFF key word is possible, becausethe void keyword \ color {# FF3030} {void keyword}V O I D OFF key word in any case will returnundefined \ color {# FF3030} {undefined:
var obj;
obj === void(0);
This is also often used in actual development void (0) \color{#FF3030}{void(0)}void(0)来代替 u n d e f i n e d \color{#FF3030}{undefined} The reason for u n d e f i n e d .