为什么 undefined 在 JavaScript 中不可写?
Why is undefined not writable in JavaScript?
根据MDN's documentation on undefined
:
In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.
undefined 的 属性 属性之一是不可写。
但如果我这样做:
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
//string
这是否意味着我可以覆盖 undefined
的值?如果有人这样做会怎样? JavaScript 应该对此发出警告吗?
Amy 删除的评论给出了解决方案。您正在创建一个名为 undefined
的变量,如果您在全局范围内执行代码片段,它就不起作用:
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
但如果您在 undefined 不再引用全局变量的局部范围内执行此操作,它会有效地工作:
(()=>{
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
})()
为了防止这个错误,你可以使用'use strict';
指令:
'use strict';
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
如果您正在编写无法控制执行位置的代码(例如库、嵌入等),那么使用 IIFE 是一个很好的模式,这样您就可以保证 undefined 永远是correct/usable。这是一个例子:
(function(undefined){
// undefined will equal `undefined` because we didn't pass in an argument to the IIFE
console.log(undefined); // always `undefined`, even if undefined !== `undefined`outside of the IIFE scope
})(); // No argument supplied
我的所有测试都是在 Ubuntu 上使用 Firefox 72.0b10(64 位)进行的,第一个片段的结果在旧版浏览器上可能会有所不同。
While it is possible to use it as an identifier (variable name) in any
scope other than the global scope (because undefined is not a reserved
word), doing so is a very bad idea that will make your code difficult
to maintain and debug.
来源https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
undefined
未保留 因此可以为其分配新值。
当您不使用 strict mode
时,您实际上是在本地范围内创建一个名为 undefined
的变量并为其分配一个 string
值。
undefined
is a property of the global object。 undefined 的初始值为原始值 undefined
.
use strict
将有助于防止在全局范围内出现此错误,同时它仍然可以在本地范围内被覆盖。
如果你想更安全,你应该使用 void 0
而不是 undefined
总是 returns undefined
.
'use strict'
const test = (() => {
let undefined = "test"
console.log(typeof undefined)
console.log(typeof void 0)
})()
根据MDN's documentation on undefined
:
In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.
undefined 的 属性 属性之一是不可写。
但如果我这样做:
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
//string
这是否意味着我可以覆盖 undefined
的值?如果有人这样做会怎样? JavaScript 应该对此发出警告吗?
Amy 删除的评论给出了解决方案。您正在创建一个名为 undefined
的变量,如果您在全局范围内执行代码片段,它就不起作用:
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
但如果您在 undefined 不再引用全局变量的局部范围内执行此操作,它会有效地工作:
(()=>{
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
})()
为了防止这个错误,你可以使用'use strict';
指令:
'use strict';
var undefined = 'hello';
var test = undefined;
console.log(typeof test);
如果您正在编写无法控制执行位置的代码(例如库、嵌入等),那么使用 IIFE 是一个很好的模式,这样您就可以保证 undefined 永远是correct/usable。这是一个例子:
(function(undefined){
// undefined will equal `undefined` because we didn't pass in an argument to the IIFE
console.log(undefined); // always `undefined`, even if undefined !== `undefined`outside of the IIFE scope
})(); // No argument supplied
我的所有测试都是在 Ubuntu 上使用 Firefox 72.0b10(64 位)进行的,第一个片段的结果在旧版浏览器上可能会有所不同。
While it is possible to use it as an identifier (variable name) in any scope other than the global scope (because undefined is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.
来源https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
undefined
未保留 因此可以为其分配新值。
当您不使用 strict mode
时,您实际上是在本地范围内创建一个名为 undefined
的变量并为其分配一个 string
值。
undefined
is a property of the global object。 undefined 的初始值为原始值 undefined
.
use strict
将有助于防止在全局范围内出现此错误,同时它仍然可以在本地范围内被覆盖。
如果你想更安全,你应该使用 void 0
而不是 undefined
总是 returns undefined
.
'use strict'
const test = (() => {
let undefined = "test"
console.log(typeof undefined)
console.log(typeof void 0)
})()