Javascript “void 0”和“未定义”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4806286/
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
difference between "void 0 " and "undefined"
提问by andres descalzo
I'm using "Closure Compiler", when compiling my scripts I spend the following:
我正在使用"Closure Compiler",在编译我的脚本时,我花费了以下内容:
Before compiling:
编译前:
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print,print_input_delimiter
// ==/ClosureCompiler==
var myObj1 = (function() {
var undefined; //<----- declare undefined
this.test = function(value, arg1) {
var exp = 0;
arg1 = arg1 == undefined ? true : arg1; //<----- use declare undefined
exp = (arg1) ? value * 5 : value * 10;
return exp;
};
return this;
}).call({});
var myObj2 = (function() {
this.test = function(value, arg1) {
var exp = 0;
arg1 = arg1 == undefined ? true : arg1; //<----- without declare undefined
exp = (arg1) ? value * 5 : value * 10;
return exp;
};
return this;
}).call({});
Compiled:
编译:
// Input 0
var myObj1 = function() {
this.test = function(b, a) {
a = a == void 0 ? true : a; //<-----
var c = 0;
return c = a ? b * 5 : b * 10
};
return this
}.call({}), myObj2 = function() {
this.test = function(b, a) {
a = a == undefined ? true : a; //<-----
var c = 0;
return c = a ? b * 5 : b * 10
};
return this
}.call({});
With this I believe the question of the use of "void 0 " and "undefined", is there any difference in the use or the two cases are well?.
有了这个我相信使用“void 0”和“未定义”的问题,使用上有什么区别还是两种情况都很好?。
Edit
编辑
if I define "var undefined" compiled with "void 0 ", if I did not define "undefined" compiled with "undedined. " then not a matter of number of characters between "undefined" and "void 0"
如果我定义用“void 0”编译的“var undefined”,如果我没有定义用“undedined”编译的“undefined”。那么不是“undefined”和“void 0”之间的字符数问题
Edit II: performance, based on this link
编辑二:性能,基于此链接
IE 8:
typeof: 228ms
undefined: 62ms
void 0: 57ms
IE 8:
typeof: 228ms
undefined: 62ms
void 0: 57ms
Firefox 3.6:
typeof: 10ms
undefined: 3ms
void 0: 3ms
Firefox 3.6:
typeof:10ms
未定义:3ms
void 0:3ms
Opera 11:
typeof: 67ms
undefined: 19ms
void 0: 20ms
Opera 11:
typeof:67ms
未定义:19ms
void 0:20ms
Chrome 8:
typeof: 3ms
undefined: 5ms
void 0: 3ms
Chrome 8:
typeof:3ms
未定义:5ms
void 0:3ms
回答by Matt Ball
The
void
operator evaluates the givenexpression
and then returnsundefined
.This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.
The void operator is often used merely to obtain the
undefined
primitive value, usually using "void(0)
" (which is equivalent to "void 0
"). In these cases, the global variableundefined
can be used instead (assuming it has not been assigned to a non-default value).
该
void
运算符计算给定的expression
,然后返回undefined
。此运算符允许将产生副作用的表达式插入需要计算结果为 undefined 的表达式的位置。
void 运算符通常仅用于获取
undefined
原始值,通常使用“void(0)
”(相当于“void 0
”)。在这些情况下,undefined
可以改用全局变量(假设它没有被分配给一个非默认值)。
Closure Compiler swaps in void 0
because it contains fewer characters than undefined
, therefore producing equivalent, smaller code.
Closure Compiler 换入,void 0
因为它包含的字符少于undefined
,因此产生等效的、更小的代码。
Re: OP comment
回复:OP评论
yes, I read the documentation, but in the example I gave, "google closure" in a case using "void 0" and another "undefined"
是的,我阅读了文档,但在我给出的示例中,在使用“void 0”和另一个“未定义”的情况下,“谷歌关闭”
I believe this is actually a bug in Google Closure Compiler!
我相信这实际上是Google Closure Compiler 中的一个错误!
回答by CMS
The real only semantic difference between void expr
and undefined
is that on ECMAScript 3, the undefined
property of the global object (window.undefined
on browser environments) is writable, whereas the void
operator will return the undefined
value always.
void expr
和之间真正唯一的语义区别undefined
是,在ECMAScript 3 上,undefined
全局对象的属性(window.undefined
在浏览器环境中)是可写的,而void
运算符将始终返回undefined
值。
A popular pattern that is often implemented, to use undefined
without worries is simply declaring an argument, and not passing anything to it:
一种经常实现的流行模式,可以放心使用undefined
,只需声明一个参数,而不向它传递任何内容:
(function (undefined) {
//...
if (foo !== undefined) {
// ...
}
})();
That will allow minifiers to shrink the argument maybe to a single letter (even shorter than void 0
:), e.g.:
这将允许缩小器将参数缩小为单个字母(甚至比void 0
:),例如:
(function (a) {
//...
if (foo !== a) {
// ...
}
})();
回答by Stephen Chung
Just a follow-up on all the answers before.
只是对之前所有答案的跟进。
They look the same, but to the Compiler they are completely different.
它们看起来一样,但对编译器来说它们是完全不同的。
The two code sections compile to different outputs because one is referring to a local variable (the var undefined), and the compiler simply in-lines it because it is used exactly once and is no more than one line. If it is used more than once, then this in-lining won't happen. The in-lining provides a result of "undefined", which is shorter to represent as "void 0".
这两个代码段编译为不同的输出,因为一个是指一个局部变量(var undefined),编译器只是简单地将它内联,因为它只使用了一次并且不超过一行。如果多次使用,则不会发生这种内联。内联提供了“未定义”的结果,用较短的时间表示为“void 0”。
The one without a local variable is referring to the variable called "undefined" under the global object, which is automatically "extern'ed" by the Closure Compiler (in fact, all global object properties are). Therefore, no renaming takes place, and no in-lining takes place. Voila! still "undefined".
没有局部变量的那个是指全局对象下名为“undefined”的变量,它被闭包编译器自动“extern'ed”(实际上,所有全局对象属性都是)。因此,不会发生重命名,也不会发生内联。瞧!仍然“未定义”。
回答by jAndy
There is no difference, Try it yourself:
没有区别,自己试试吧:
void 0 === undefined
will evaluate to true
.undefined
is 3characters longer, I guess that is the reason why they use it that way.
将评估为true
. undefined
长了3 个字符,我想这就是他们以这种方式使用它的原因。