Javascript ‘void 0’是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7452341/
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
What does `void 0` mean?
提问by Randomblue
Reading through the Backbone.js source code, I saw this:
阅读 Backbone.js 源代码,我看到了这个:
validObj[attr] = void 0;
What is void 0
? What is the purpose of using it here?
什么是void 0
?在这里使用它的目的是什么?
回答by Peter Olson
What does void 0
mean?
什么void 0
意思?
void
[MDN]is a prefix keyword that takes one argument and always returns undefined
.
void
[MDN]是一个前缀关键字,它接受一个参数并始终返回undefined
.
Examples
例子
void 0
void (0)
void "hello"
void (new Date())
//all will return undefined
What's the point of that?
这样做有什么意义?
It seems pretty useless, doesn't it? If it always returns undefined
, what's wrong with just using undefined
itself?
看起来很没用,不是吗?如果它总是返回undefined
,那么只使用undefined
它自己有什么问题?
In a perfect world we would be able to safely just use undefined
: it's much simpler and easier to understand than void 0
. But in case you've never noticed before, this isn't a perfect world, especially when it comes to Javascript.
在一个完美的世界中,我们将能够安全地使用undefined
:它比void 0
. 但是,如果您以前从未注意到,这并不是一个完美的世界,尤其是在 Javascript 方面。
The problem with using undefined
was that undefined
is not a reserved word (it is actually a property of the global object [wtfjs]). That is, undefined
is a permissible variable name, so you could assign a new value to it at your own caprice.
using 的问题undefined
在于undefined
它不是一个保留字(它实际上是全局对象[wtfjs] 的一个属性)。也就是说,undefined
是一个允许的变量名,因此您可以随意为它分配一个新值。
alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"
Note: This is no longer a problem in any environment that supports ECMAScript 5 or newer (i.e. in practice everywhere but IE 8), which defines the undefined
property of the global object as read-only (so it is only possible to shadow the variable in your own local scope). However, this information is still useful for backwards-compatibility purposes.
注意:这在任何支持 ECMAScript 5 或更新版本的环境中都不再是问题(即实际上除了 IE 8 之外的所有地方),它将undefined
全局对象的属性定义为只读(因此只能在您自己的本地范围)。但是,此信息对于向后兼容目的仍然有用。
alert(window.hasOwnProperty('undefined')); // alerts "true"
alert(window.undefined); // alerts "undefined"
alert(undefined === window.undefined); // alerts "true"
var undefined = "new value";
alert(undefined); // alerts "new value"
alert(undefined === window.undefined); // alerts "false"
void
, on the other hand, cannot be overidden. void 0
will alwaysreturn undefined
. undefined
, on the other hand, can be whatever Mr. Javascript decides he wants it to be.
void
,另一方面,不能被覆盖。void 0
将始终返回undefined
。undefined
,另一方面,可以是 Javascript 先生决定他想要的任何东西。
Why void 0
, specifically?
为什么void 0
,特别是?
Why should we use void 0
? What's so special about 0
? Couldn't we just as easily use 1
, or 42
, or 1000000
or "Hello, world!"
?
为什么要使用void 0
?有什么特别的0
?我们不能同样轻松地使用1
, or 42
, or 1000000
or"Hello, world!"
吗?
And the answer is, yes, we could, and it would work just as well. The only benefit of passing in 0
instead of some other argument is that 0
is short and idiomatic.
答案是,是的,我们可以,而且效果也很好。传入0
而不是其他一些参数的唯一好处0
是简短而惯用。
Why is this still relevant?
为什么这仍然相关?
Although undefined
can generally be trusted in modern JavaScript environments, there is one trivial advantage of void 0
: it's shorter. The difference is not enough to worry about when writing code but it can add up enough over large code bases that most code minifiers replace undefined
with void 0
to reduce the number of bytes sent to the browser.
虽然undefined
在现代 JavaScript 环境中通常可以信任,但void 0
它有一个微不足道的优势:它更短。在编写代码时,这种差异还不够,但它可以在大多数代码压缩器替换的大型代码库上加起来足够多undefined
,void 0
以减少发送到浏览器的字节数。
回答by epascarello
void 0
returns undefined and can not be overwritten while undefined
can be overwritten.
void 0
返回 undefined 并且不能被覆盖,而undefined
可以被覆盖。
var undefined = "HAHA";