J'utilise "Compilateur de fermetures" En compilant mes scripts, je passe ce qui suit :
Avant de 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({});
Compilé :
// 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({});
Avec ceci je crois que la question de l'utilisation de "void 0" et "undefined", y a-t-il une différence dans l'utilisation ou les deux cas sont bien ?
Modifier
si je définis "var undefined" compilé avec "void 0 ", si je n'ai pas défini "undefined" compilé avec "undedined. "alors ce n'est pas une question de nombre de caractères entre "undefined" et "void 0".
Edit II : performance, basée sur ce lien
IE 8 :
typeof : 228ms
indéfini : 62ms
void 0 : 57ms
Firefox 3.6 :
typeof : 10ms
indéfini : 3ms
void 0 : 3ms
Opéra 11 :
typeof : 67ms
indéfini : 19ms
void 0 : 20ms
Chrome 8 :
typeof : 3ms
indéfini : 5ms
void 0 : 3ms