什么是`无效0`是什么意思? [英] What does `void 0` mean?
问题描述
可能重复:结果
什么“的javascript:无效(0)rdquo;的是什么意思?
通过Backbone.js的源$ C $ C阅读:
Reading through the Backbone.js source code:
validObj[attr] = void 0;
什么是 0无效
?这到底是怎么使用它的目的是什么?
What is void 0
? What is the purpose of using it here?
推荐答案
<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/void_Operator\"><$c$c>void$c$c>[MDN]是它有一个参数,并始终是preFIX关键字返回未定义
。
例子
void 0
void (0)
void "hello"
void (new Date())
//all will return undefined
什么的地步?
这似乎pretty没用,不是吗?如果它总是返回未定义
,这有什么错只使用未定义
本身?
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?
在一个完美的世界里,我们将能够安全地只使用未定义
:它比无效0 $ C更简单,更容易理解$ C>。但如果你以前从来没有注意到,的这不是一个完美的世界的,特别是当它涉及到的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.
使用问题未定义
是未定义
不是保留字(的it其实是全局对象的属性 [wtfjs] )。也就是说,未定义
是允许的变量名,所以你可以在你自己的任性分配一个新的值了。
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),它定义了在未定义
属性作为全局对象只读(因此只可能阴影自己的局部范围内的变量)。但是,此信息仍然是有用的向后兼容的目的。
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"
无效
,在另一方面,不能overidden。 0无效
将总是的回报未定义
。 未定义
,在另一方面,可以不管的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.
为什么要使用 0无效
?有什么特别之处 0
?我们能不能很容易地使用 1
或 42
或百万
或你好,世界!
?
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环境是可信的,有一个无效0
有细微的优势:它是短。所不同的是没有足够的理由担心写code时,但它可以在大code碱基,大部分code minifiers替换未定义
有足够的加起来 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.
这篇关于什么是`无效0`是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!