undefined
即是一个原始数据类型,也是一个原始值数据
undefined
是全局对象上的一个属性
console.log(window.undefined) // undefined
复制代码
writable: false
window.undefined = 1; // 不可写
console.log(window.undefined) // undefined
复制代码
configurable: false
delete window.undefined;
console.log(window.undefined) // 还是正常打印出undefined
复制代码
enumerable: false
for(var k in window){
if(k === undefined){
console.log(k); // 没有打印
}
}
复制代码
Object.defineProperty()
报错了Object.defineProperty(window, 'undefined', {
enumerable: true,
writable: true,
configurable: true
})
复制代码
undefined
,类型也是 undefined
var a;
console.log(a); // undefined
function test(a){
console.log(typeof a); // 类型undefined
return a;
}
复制代码
undefined
console.log(test()); // undefined
复制代码
undefined
function test2(){
console.log(123);
// return undefined
}
console.log(test2()); // undefined
复制代码
undefined
不能作为变量使用var undefined = 1;
console.log(undefined); // undefined
复制代码
undefined
不是JS的保留字和关键字,在全局下不可写,但是在局部作为变量时,它不会去全局找,局部作用域可以打印出 1
,就算在严格模式下也仍然可以function test3(){
'use strict'; // 严格模式
var undefined = 1;
console.log(undefined); // 1
}
test3();
复制代码
void(0)
对0进行求值,始终返回 undefined
var a, b, c;
a = void(b = 1, c = 2);
console.log(a, b, c); // undefined 1 2
// <a href="javascript:void(0)"> // 返回 undefined 阻止 <a> 标签跳转
console.log(void(0) === window.undefined); // true void(0) 全等于 window.undefined
复制代码
undefined
,老的程序会用void(0)
返回的undefined
来作为 undefined
去和其他变量判断是否等于 undefined
function test4(){
var undefined = 1;
console.log(undefined); // 1
console.log(void(0)); // undefined
console.log(void(0) === undefined); // false, 因为 undefined 可以在局部作用域中作为变量使用
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。