‘void 0’是什么意思? [英] What does `void 0` mean?
问题描述
阅读 Backbone.js 源代码,我看到了:
Reading through the Backbone.js source code, I saw this:
validObj[attr] = void 0;
什么是void 0
?在这里使用它的目的是什么?
What is void 0
? What is the purpose of using it here?
推荐答案
void 0
是什么意思?
void
[MDN] 是一个前缀关键字,它接受一个参数并且总是返回 undefined
.
示例
void 0
void (0)
void "hello"
void (new Date())
//all will return undefined
这有什么意义?
这看起来很没用,不是吗?如果它总是返回 undefined
,那么只使用 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
:它比 void 0
更简单易懂.但是,如果您以前从未注意到,这不是一个完美的世界,尤其是在 Javascript 方面.
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
的问题在于 undefined
不是保留字 (其实是全局对象的一个属性[wtfjs]).也就是说,undefined
是一个允许的变量名,因此您可以随意为其分配一个新值.
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.
alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"
注意:这在任何支持 ECMAScript 5 或更新版本的环境中都不再是问题(即实际上除了 IE 8 之外的所有地方),它将全局对象的 undefined
属性定义为只读(因此只能在您自己的本地范围内隐藏变量).但是,此信息对于向后兼容目的仍然很有用.
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.
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
另一方面,不能被覆盖.void 0
将总是返回 undefined
.另一方面,undefined
可以是 Javascript 先生决定的任何内容.
void
, on the other hand, cannot be overidden. void 0
will always return undefined
. undefined
, on the other hand, can be whatever Mr. Javascript decides he wants it to be.
为什么要使用void 0
?0
有什么特别之处?难道我们不能简单地使用 1
,或 42
,或 1000000
或 Hello, world!"代码>?
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!"
?
答案是,是的,我们可以,而且效果也很好.传入 0
而不是其他参数的唯一好处是 0
简短且符合习惯.
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.
尽管在现代 JavaScript 环境中通常可以信任 undefined
,但 void 0
有一个微不足道的优势:它更短.在编写代码时,差异还不够,但它可以在大型代码库中累积足够多,以至于大多数代码压缩器将 undefined
替换为 void 0
以减少字节数发送到浏览器.
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.
这篇关于‘void 0’是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!