本文翻译自:What does `void 0` mean? [duplicate]
Possible Duplicate: 可能重复:
What does “javascript:void(0)” mean? “ javascript:void(0)”是什么意思?
Reading through the Backbone.js source code, I saw this: 通过阅读Backbone.js源代码,我看到了以下内容:
validObj[attr] = void 0;
What is void 0
? 什么是void 0
? What is the purpose of using it here? 在这里使用它的目的是什么?
#1楼
参考:https://stackoom.com/question/VGh3/void-是什么意思-重复
#2楼
void
is a reserved JavaScript keyword. void
是保留的JavaScript关键字。 It evaluates the expression and always returns undefined
. 它计算表达式并始终返回undefined
。
#3楼
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
本身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
. 在一个完美的世界中,我们将能够安全地使用undefined
:与void 0
相比,它更容易理解。 But in case you've never noticed before, this isn't a perfect world , especially when it comes to Javascript. 但是,如果您以前从未注意到过, 这不是一个完美的世界 ,尤其是在涉及Javascript时。
The problem with using undefined
was that undefined
is not a reserved word ( it is actually a property of the global object [wtfjs] ). 使用undefined
的问题是undefined
不是保留字( 它实际上是全局对象[wtfjs]的属性 )。 That is, undefined
is a permissible variable name, so you could assign a new value to it at your own caprice. 也就是说, 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 (ie 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). 注意:在任何支持ECMAScript 5或更高版本的环境(即,除IE 8之外,实际上在所有地方都可以使用)中,这不再是问题,该环境将全局对象的undefined
属性定义为只读(因此,只能对变量进行阴影处理)在您自己的本地范围内)。 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
, on the other hand, cannot be overidden. 另一方面, void
不能被覆盖。 void 0
will always return undefined
. void 0
将始终返回undefined
。 undefined
, on the other hand, can be whatever Mr. Javascript decides he wants it to be. 另一方面, undefined
可以是Javascript先生决定的任何类型。
Why void 0
, specifically? 为什么void 0
?
Why should we use void 0
? 为什么要使用void 0
? What's so special about 0
? 0
什么特别之处? Couldn't we just as easily use 1
, or 42
, or 1000000
or "Hello, world!"
我们不能像1
或42
或1000000
或"Hello, world!"
那样轻松地使用它"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. 尽管在现代JavaScript环境中通常可以信任undefined
,但void 0
有一个琐碎的优点:它更短。 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
替换为void 0
以减少发送到浏览器的字节数。
#4楼
void 0
返回undefined且不能覆盖,而undefined
可以覆盖。
var undefined = "HAHA";