“void 0”之间的差异和“未定义的” [英] difference between "void 0 " and "undefined"

查看:249
本文介绍了“void 0”之间的差异和“未定义的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Closure Compiler,在编译我的脚本时,我会花费以下内容:

I'm using "Closure Compiler", when compiling my scripts I spend the following:

编译前:

// ==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的问题,有吗使用上的任何差异或两种情况都很好吗?

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?.

编辑

如果我定义用void 0编译的var undefined,如果我没有定义用undedined编译的undefined,那么undefined和void 0之间的字符数不是问题

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"

测试

编辑II:性能,基于此链接

Edit II: performance, based on this link

代码与测试

IE 8:

typeof:228ms

undefined:62ms

无效0:57ms

IE 8:
typeof: 228ms
undefined: 62ms
void 0: 57ms

Firefox 3.6:

typeof: 10ms

undefined:3ms

v oid 0:3ms

Firefox 3.6:
typeof: 10ms
undefined: 3ms
void 0: 3ms

Opera 11:

typeof:67ms

undefined:19ms

void 0:20ms

Opera 11:
typeof: 67ms
undefined: 19ms
void 0: 20ms

Chrome 8:

typeof:3ms

undefined:5ms

void 0:3ms

Chrome 8:
typeof: 3ms
undefined: 5ms
void 0: 3ms

推荐答案

来自MDN


void 运算符计算给定的 表达式 然后返回 undefined

The void operator evaluates the given expression and then returns undefined.

此运算符允许将产生副作用的表达式插入到需要求值为undefined的表达式的位置。

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

void运算符通常仅用于获取 undefined 原语值,通常使用 void(0)(这相当于 void 0 )。在这些情况下,可以使用全局变量 undefined (假设它尚未分配给非默认值)。

The void operator is often used merely to obtain the undefined primitive value, usually using "void(0)" (which is equivalent to "void 0"). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

Closure Compiler交换 void 0 ,因为它包含的字符少于 undefined 因此生成相同的,更小的代码

Closure Compiler swaps in void 0 because it contains fewer characters than undefined, therefore producing equivalent, smaller code.

Re:OP评论


是的,我阅读了文档,但在我给出的示例中,google closure在使用void 0和另一个undefined的情况下

yes, I read the documentation, but in the example I gave, "google closure" in a case using "void 0" and another "undefined"

我认为这实际上是 Google Closure编译器中的错误

这篇关于“void 0”之间的差异和“未定义的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆