Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - “ void 0”是什么意思? [重复](What does `void 0` mean? [duplicate])

Possible Duplicate:

(可能重复:)

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?

(在这里使用它的目的是什么?)

  ask by Randomblue translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!"

(我们不能像1421000000"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以减少发送到浏览器的字节数。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...