sto usando "Closure Compiler", quando si compila miei script passo la seguente:differenza tra "vuoto 0" e "indefinito"
Prima di compilare:
// ==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({});
compilato:
// 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({});
Con questo credo che la domanda sull'uso di "void 0" e "undefined", c'è qualche differenza nell'uso o i due casi sono buoni ?.
Modifica
se io definisco "var indefinito" compilato con "vuoto 0", se non mi definisco "indefinito" compilato con "undedined.", Quindi non è una questione di numero di caratteri tra "indefinito "e "vuoto 0"
Edit II: prestazioni, sulla base di this link
IE 8:
typeof: 228ms
indefinito: 62ms
vuoto 0: 57ms
Firefox 3.6:
typeof: 10ms
indefinito: 3ms
vuoto 0: 3ms
Opera 11:
typeof: 67ms
indefinito: 19ms
vuoto 0: 20 ms
Chrome 8:
typeof: 3ms
indefinito: 5ms
vuoto 0: 3ms
sì, ho letto la documentazione, ma nell'esempio che ho dato, "google closure" in un caso usando "void 0" e un altro "indefinito" –
@andres: hmmm, capisco cosa stai dicendo. Questo è strano ... –
@andres: guarda la mia modifica. –