我在用着“关闭编译器”,在编译我的脚本时,我花费了以下内容:
编译前:
// ==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”的使用问题,使用上有什么区别或者两种情况都很好吗?
Edit
如果我定义用“void 0”编译的“var undefined”,如果我没有定义用“undedined.”编译的“undefine”,那么就不是“undefine”和“void 0”之间的字符数问题
Test
编辑二:性能,基于这个链接
代码和测试
IE 8:
类型:228ms
未定义:62ms
无效 0:57 毫秒
火狐 3.6:
类型:10ms
未定义:3ms
无效0:3ms
歌剧 11:
类型:67ms
未定义:19ms
无效0:20ms
铬8:
类型:3ms
未定义:5ms
无效0:3ms