“void 0”与“void 0”之间的差异和“未定义”

发布于 2024-10-14 19:45:30 字数 2137 浏览 7 评论 0原文

我正在使用 "Closure 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({});

编译:

// 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”的使用问题,在使用上有什么区别或者两种情况都很好吗?

编辑

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

测试

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

代码和测试

IE 8:
类型:228ms
未定义:62ms
void 0: 57ms

Firefox 3.6:
类型:10ms
未定义:3ms
void 0: 3ms

Opera 11:
类型:67ms
未定义:19ms
void 0: 20ms

Chrome 8:
类型:3ms
未定义:5ms
无效 0: 3ms

I'm using "Closure Compiler", when compiling my scripts I spend the 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) {
    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({});

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

Edit

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"

Test

Edit II: performance, based on this link

Code and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滥情稳全场 2024-10-21 19:45:30

来自 MDN

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

此运算符允许将产生副作用的表达式插入到需要计算结果为未定义的表达式的位置。

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

闭包编译器会交换 void 0,因为它包含的字符比 undefined 少,因此生成等效的、更小的代码


回复:OP评论

是的,我阅读了文档,但在我给出的示例中,在使用“void 0”和另一个“未定义”的情况下“googleclosure”

我相信这实际上是一个Google Closure 编译器中的错误

From MDN:

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

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

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 swaps in void 0 because it contains fewer characters than undefined, therefore producing equivalent, smaller code.


Re: OP comment

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

I believe this is actually a bug in Google Closure Compiler!

一直在等你来 2024-10-21 19:45:30

void exprundefined 之间真正唯一的语义区别在于,在 ECMAScript 3 上,全局变量的 undefined 属性对象(在浏览器环境中为 window.undefined)是可写的,而 void 运算符将始终返回未定义的值。

经常实现的一种流行模式,使用 undefined 不用担心,只需声明一个参数,而不向其传递任何内容:

(function (undefined) {
  //...
  if (foo !== undefined) {
    // ...
  }

})();

这将允许缩小器将参数缩小到单个字母(甚至短于void 0 :),例如:

(function (a) {
  //...
  if (foo !== a) {
    // ...
  }
})();

The real only semantic difference between void expr and undefined is that on ECMAScript 3, the undefined property of the global object (window.undefined on browser environments) is writable, whereas the void operator will return the undefined value always.

A popular pattern that is often implemented, to use undefined without worries is simply declaring an argument, and not passing anything to it:

(function (undefined) {
  //...
  if (foo !== undefined) {
    // ...
  }

})();

That will allow minifiers to shrink the argument maybe to a single letter (even shorter than void 0 :), e.g.:

(function (a) {
  //...
  if (foo !== a) {
    // ...
  }
})();
雪若未夕 2024-10-21 19:45:30

只是之前所有答案的后续。

它们看起来是一样的,但对于编译器来说它们是完全不同的。

两个代码段编译为不同的输出,因为其中一个代码段引用局部变量(var 未定义),而编译器只是将其内联,因为它仅使用一次且不超过一行。如果多次使用,则不会发生这种内联。内联提供“未定义”的结果,更短地表示为“void 0”。

没有局部变量的是指全局对象下名为“未定义”的变量,该变量会被闭包编译器自动“extern”(实际上,所有全局对象属性都是)。因此,不会发生重命名,也不会发生内联。瞧!仍然“未定义”。

Just a follow-up on all the answers before.

They look the same, but to the Compiler they are completely different.

The two code sections compile to different outputs because one is referring to a local variable (the var undefined), and the compiler simply in-lines it because it is used exactly once and is no more than one line. If it is used more than once, then this in-lining won't happen. The in-lining provides a result of "undefined", which is shorter to represent as "void 0".

The one without a local variable is referring to the variable called "undefined" under the global object, which is automatically "extern'ed" by the Closure Compiler (in fact, all global object properties are). Therefore, no renaming takes place, and no in-lining takes place. Voila! still "undefined".

眼角的笑意。 2024-10-21 19:45:30

没有什么区别,自己尝试一下:

void 0 === undefined

将评估为 true
undefined 长了 3 个字符,我想这就是他们这样使用它的原因。

There is no difference, Try it yourself:

void 0 === undefined

will evaluate to true.
undefined is 3 characters longer, I guess that is the reason why they use it that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文