“void 0”与“void 0”之间的差异和“未定义”
我正在使用 "Closure Compiler",在编译我的脚本时,我会执行以下操作:
编译之前:
// ==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({});
编译:
// 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({});
有了这个我相信“void 0”和“undefined”的使用问题,在使用上有什么区别或者两种情况都很好吗?
编辑
如果我定义用“void 0”编译的“var undefined”,如果我没有定义用“undedined”编译的“undefine”。那么就不是“undefine”和“之间的字符数问题” void 0"
编辑二:性能,基于此链接
IE 8:
类型:228ms
未定义:62ms
void 0: 57ms
Firefox 3.6:
类型:10ms
未定义:3ms
void 0: 3ms
Opera 11:
类型:67ms
未定义:19ms
void 0: 20ms
Chrome 8:
类型:3ms
未定义:5ms
无效 0: 3ms
I'm using "Closure Compiler", when compiling my scripts I spend the following:
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?.
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"
Edit II: performance, based on this link
IE 8:
typeof: 228ms
undefined: 62ms
void 0: 57ms
Firefox 3.6:
typeof: 10ms
undefined: 3ms
void 0: 3ms
Opera 11:
typeof: 67ms
undefined: 19ms
void 0: 20ms
Chrome 8:
typeof: 3ms
undefined: 5ms
void 0: 3ms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 MDN:
闭包编译器会交换
void 0
,因为它包含的字符比undefined
少,因此生成等效的、更小的代码。回复:OP评论
我相信这实际上是一个Google Closure 编译器中的错误!
From MDN:
Closure Compiler swaps in
void 0
because it contains fewer characters thanundefined
, therefore producing equivalent, smaller code.Re: OP comment
I believe this is actually a bug in Google Closure Compiler!
void expr
和undefined
之间真正唯一的语义区别在于,在 ECMAScript 3 上,全局变量的undefined
属性对象(在浏览器环境中为window.undefined
)是可写的,而void
运算符将始终返回未定义的值。经常实现的一种流行模式,使用
undefined
不用担心,只需声明一个参数,而不向其传递任何内容:这将允许缩小器将参数缩小到单个字母(甚至短于
void 0
:),例如:The real only semantic difference between
void expr
andundefined
is that on ECMAScript 3, theundefined
property of the global object (window.undefined
on browser environments) is writable, whereas thevoid
operator will return the undefined value always.A popular pattern that is often implemented, to use
undefined
without worries is simply declaring an argument, and not passing anything to it:That will allow minifiers to shrink the argument maybe to a single letter (even shorter than
void 0
:), e.g.:只是之前所有答案的后续。
它们看起来是一样的,但对于编译器来说它们是完全不同的。
两个代码段编译为不同的输出,因为其中一个代码段引用局部变量(var 未定义),而编译器只是将其内联,因为它仅使用一次且不超过一行。如果多次使用,则不会发生这种内联。内联提供“未定义”的结果,更短地表示为“void 0”。
没有局部变量的是指全局对象下名为“未定义”的变量,该变量会被闭包编译器自动“extern”(实际上,所有全局对象属性都是)。因此,不会发生重命名,也不会发生内联。瞧!仍然“未定义”。
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".
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".
没有什么区别,自己尝试一下:
将评估为
true
。undefined
长了 3 个字符,我想这就是他们这样使用它的原因。There is no difference, Try it yourself:
will evaluate to
true
.undefined
is 3 characters longer, I guess that is the reason why they use it that way.