在 Javascript Garden 里面读到如下一段话:The language also defines a global variable that has the value of undefined; this variable is also called undefined.
However, this variable is neither a constant nor a keyword of the language. This means that its value can be easily overwritten.我的理解是:Javascript 还定义了一个全局变量 undefined,它的值就是 undefined,然而这个变量既不是常量也不是 Javascript 的关键字。这就意味着这个变量的值可以被重新赋值。但是我测试后发现:重新赋值后,全局变量 undefined 的值并没有被重写,是不是我误解了原文的意思呢?
3 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
ECMAScript 5 的标准里加入了变量拥有可写、可枚举、可配置属性。
全局的 undefined 的可写属性应该是 false 的。
Object.getOwnPropertyDescriptor(window, "undefined");
这句会得到结果:
Object {value: undefined, writable: false, enumerable: false, configurable: false}所以写不了。
暮色呼如
TA贡献1853条经验 获得超9个赞
这里的重写应该是指在非全局作用域里的重写,比如
(function(){
var undefined = 123;
console.log(undefined);//输出123
})()
像null就不会这样
(function(){
var null = 123;//Error
console.log(null);
})()
说明undefined和null不是一个级别的
至于undefined全局为什么会重写失败,就不是我这种菜鸟懂的了,还望高手指点
添加回答
举报
0/150
提交
取消
