如何将 javascript var 设置为未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5795936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a javascript var as undefined
提问by AnApprentice
Given:
鉴于:
console.log(boo); this outputs undefined
Given:
鉴于:
var boo = 1;
console.log(boo); this outputs 1
After defining boo and setting to 1, how can I then reset boo, so that console.log outputs undefined?
定义 boo 并设置为 1 后,如何重置 boo,以便 console.log 输出未定义?
Thanks
谢谢
采纳答案by neebz
delete boo
delete boo
Don't use var boo = undefined
. undefined is just a variable and if someone sets undefined = "hello"
then you'll be getting hello everywhere :)
不要使用var boo = undefined
. undefined 只是一个变量,如果有人设置,undefined = "hello"
那么你会到处打招呼:)
EDIT:
编辑:
null wasn't same as undefined. removed that bit.
null 与 undefined 不同。删除了那一点。
回答by Gooseberry
Solution
解决方案
To reliably set a variable boo
to undefined
, use a function with an empty return
expression:
要可靠地将变量设置boo
为undefined
,请使用带有空return
表达式的函数:
boo = (function () { return; })();
After executing this line of code, typeof(boo)
evaluates to 'undefined'
, regardless of whether or not the undefined
global property has been set to another value. For example:
执行这行代码后,typeof(boo)
计算结果为'undefined'
,无论undefined
全局属性是否已设置为另一个值。例如:
undefined = 'hello';
var boo = 1;
console.log(boo); // outputs '1'
boo = (function () { return; })();
console.log(boo); // outputs 'undefined'
console.log(undefined); // outputs 'hello'
EDITBut see also @Colin's simpler solution!
编辑但另请参阅@Colin 的更简单的解决方案!
Reference
参考
This behavior is standard as far back as ECMAScript 1. The relevant specification states in part:
这种行为早在 ECMAScript 1 时就已成为标准。相关规范部分说明:
Syntax
return
[no LineTerminatorhere] Expression;Semantics
A
return
statement causes a function to cease execution and return a value to the caller. If Expressionis omitted, the return value isundefined
.
句法
return
[此处没有LineTerminator]表达式;语义
一个
return
语句导致函数停止执行,并返回一个值给调用者。如果省略Expression,则返回值为undefined
。
To view the original specifications, refer to:
要查看原始规格,请参阅:
Appendix
附录
For completeness, I have appended a brief summary of alternate approaches to this problem, along with objections to these approaches, based on the answers and comments given by other responders.
为完整起见,我根据其他响应者的回答和评论,附上了解决此问题的替代方法的简短摘要,以及对这些方法的反对意见。
1. Assign undefined
to boo
1. 分配undefined
给boo
boo = undefined; // not recommended
Although it is simpler to assign undefined
to boo
directly, undefined
is not a reserved word and could be replacedby an arbitrary value, such as a number or string.
虽然这是更简单的分配来undefined
向boo
直接,undefined
不是保留字和可以被取代由一个任意值,诸如数字或字符串。
2. Delete boo
2. 删除 boo
delete boo; // not recommended
Deleting boo
removes the definition of boo
entirely, rather than assigning it the value undefined
, and even then only worksif boo
is a global property.
删除boo
会boo
完全删除 的定义,而不是为其分配 value undefined
,即使如此,也仅在boo
是全局属性时才有效。
回答by Colin
Use the void
operator. It will evaluate it's expression and then return undefined
. It's idiomatic to use void 0
to assign a variable to undefined
使用void
运算符。它将评估它的表达式,然后返回undefined
。void 0
给变量赋值是惯用的方式undefined
var boo = 1; // boo is 1
boo = void 0; // boo is now undefined
Learn more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
在此处了解更多信息:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
回答by Bryan Kyle
You can simply assign a variable the value of undefined:
您可以简单地将未定义的值分配给变量:
boo = undefined;
Alternatively, you can use the delete
operator to delete the variable:
或者,您可以使用delete
运算符删除变量:
delete boo;
回答by lobster1234
This works on Chrome Javascript Console:
这适用于 Chrome Javascript 控制台:
delete(boo)
回答by McStretch
var boo = 1;
console.log(boo); // prints 1
boo = undefined;
console.log(boo); // now undefined