在 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 的值并没有被重写,是不是我误解了原文的意思呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
ECMAScript 5的标准里加入了变量拥有可写、可枚举、可配置属性。全局的
undefined的可写属性应该是false的。这句会得到结果:
所以写不了。
这里的重写应该是指在非全局作用域里的重写,比如
像null就不会这样
说明undefined和null不是一个级别的
至于undefined全局为什么会重写失败,就不是我这种菜鸟懂的了,还望高手指点
其实是可以被改的,只是看浏览器的兼容性。如下表:
