javascript - difference between "void 0 " and "undefined" -


i'm using "closure compiler", when compiling scripts spend 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) {     = == void 0 ? true : a;  //<-----     var c = 0;     return c = ? b * 5 : b * 10   };   return }.call({}), myobj2 = function() {   this.test = function(b, a) {     = == undefined ? true : a; //<-----     var c = 0;     return c = ? b * 5 : b * 10   };   return }.call({}); 

with believe question of use of "void 0 " , "undefined", there difference in use or 2 cases well?.

edit

if define "var undefined" compiled "void 0 ", if did not define "undefined" compiled "undedined. " not matter of number of characters between "undefined" , "void 0"

test

edit ii: performance, based on this link

code , test

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

from mdc:

the void operator evaluates given expression , returns undefined.
... operator allows inserting expressions produce side effects places expression evaluates undefined desired.

the void operator used merely obtain undefined primitive value, using "void(0)" (which equivalent "void 0"). in these cases, global variable undefined can used instead (assuming has not been assigned non-default value).

closure compiler swaps in void 0 because contains fewer characters undefined, therefore producing equivalent, smaller code.


re: op comment

yes, read documentation, in example gave, "google closure" in case using "void 0" , "undefined"

i believe bug in google closure compiler!


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -