本文翻译自:What does `void 0` mean? [duplicate]

This question already has answers here : 这个问题已经在这里有了答案 :
Closed 8 years ago . 8年前关闭。

Possible Duplicate: 可能重复:
What does “javascript:void(0)” mean? “ javascript:void(0)”是什么意思?

Reading through the Backbone.js source code, I saw this: 通过阅读Backbone.js源代码,我看到了以下内容:

validObj[attr] = void 0;

What is void 0 ? 什么是void 0 What is the purpose of using it here? 在这里使用它的目的是什么?


#1楼

参考:https://stackoom.com/question/VGh3/void-是什么意思-重复


#2楼

void is a reserved JavaScript keyword. void是保留的JavaScript关键字。 It evaluates the expression and always returns undefined . 它计算表达式并始终返回undefined


#3楼

What does void 0 mean? void 0是什么意思?

void [MDN] is a prefix keyword that takes one argument and always returns undefined . void [MDN]是带一个参数的前缀关键字,始终返回undefined

Examples 例子

void 0
void (0)
void "hello"
void (new Date())
//all will return undefined

What's the point of that? 有什么意义呢?

It seems pretty useless, doesn't it? 看起来很没用,不是吗? If it always returns undefined , what's wrong with just using undefined itself? 如果它总是返回undefined ,那么仅使用undefined本身undefined什么问题呢?

In a perfect world we would be able to safely just use undefined : it's much simpler and easier to understand than void 0 . 在一个完美的世界中,我们将能够安全地使用undefined :与void 0相比,它更容易理解。 But in case you've never noticed before, this isn't a perfect world , especially when it comes to Javascript. 但是,如果您以前从未注意到过, 这不是一个完美的世界 ,尤其是在涉及Javascript时。

The problem with using undefined was that undefined is not a reserved word ( it is actually a property of the global object [wtfjs] ). 使用undefined的问题是undefined不是保留字( 它实际上是全局对象[wtfjs]的属性 )。 That is, undefined is a permissible variable name, so you could assign a new value to it at your own caprice. 也就是说, undefined是允许的变量名,因此您可以按自己的意愿为它分配一个新值。

alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"

Note: This is no longer a problem in any environment that supports ECMAScript 5 or newer (ie in practice everywhere but IE 8), which defines the undefined property of the global object as read-only (so it is only possible to shadow the variable in your own local scope). 注意:在任何支持ECMAScript 5或更高版本的环境(即,除IE 8之外,实际上在所有地方都可以使用)中,这不再是问题,该环境将全局对象的undefined属性定义为只读(因此,只能对变量进行阴影处理)在您自己的本地范围内)。 However, this information is still useful for backwards-compatibility purposes. 但是,此信息对于向后兼容的目的仍然有用。

alert(window.hasOwnProperty('undefined')); // alerts "true"
alert(window.undefined); // alerts "undefined"
alert(undefined === window.undefined); // alerts "true"
var undefined = "new value";
alert(undefined); // alerts "new value"
alert(undefined === window.undefined); // alerts "false"

void , on the other hand, cannot be overidden. 另一方面, void不能被覆盖。 void 0 will always return undefined . void 0始终返回undefined undefined , on the other hand, can be whatever Mr. Javascript decides he wants it to be. 另一方面, undefined可以是Javascript先生决定的任何类型。

Why void 0 , specifically? 为什么void 0

Why should we use void 0 ? 为什么要使用void 0 What's so special about 0 ? 0什么特别之处? Couldn't we just as easily use 1 , or 42 , or 1000000 or "Hello, world!" 我们不能像1421000000"Hello, world!"那样轻松地使用它"Hello, world!" ?

And the answer is, yes, we could, and it would work just as well. 答案是,是的,我们可以,而且效果也很好。 The only benefit of passing in 0 instead of some other argument is that 0 is short and idiomatic. 传递0而不是其他参数的唯一好处是0简短且惯用。

Why is this still relevant? 为什么这仍然有意义?

Although undefined can generally be trusted in modern JavaScript environments, there is one trivial advantage of void 0 : it's shorter. 尽管在现代JavaScript环境中通常可以信任undefined ,但void 0有一个琐碎的优点:它更短。 The difference is not enough to worry about when writing code but it can add up enough over large code bases that most code minifiers replace undefined with void 0 to reduce the number of bytes sent to the browser. 这种差异不足以在编写代码时担心,但可以在大型代码库上加起来,大多数代码压缩器将undefined替换为void 0以减少发送到浏览器的字节数。


#4楼

void 0返回undefined且不能覆盖,而undefined可以覆盖。

var undefined = "HAHA";

“ void 0”是什么意思? [重复]相关推荐

  1. void函数返回值_(*void(*)()0)() 是什么

    (*void(*)()0)() 代码分析 这是啥 这行代码,是我今天在看<C陷阱与缺陷>时看到的,一开始很不能理解.慢慢上网摸索一些后,大致理解了,现在来分享一下我所理解的这行代码. 1. ...

  2. JS中的null和undefined,undefined为啥用void 0代替?

    起因   某天,在看某位同学的js代码,代码中发现了一个奇怪的东西 void 0,虽然第一眼看不懂这是什么东西,但是根据上下文,这里应该是想判断是否等于undefined,为什么要这样写的,有什么渊源 ...

  3. 详解javascript: void(0);

    原文 简书原文:https://www.jianshu.com/p/08ae8cbeb3be 什么是javascript: void(0); 我们经常会使用到 javascript:void(0) 这 ...

  4. ((void *) 0)的含义和void的一些细节

    一.在c语言中,0是一个特殊的值,它可以表示:整型数值0,空字符,逻辑假(false).表示的东西多了,有时候不好判断.尤其是空字符和数字0之间. 为了明确的指出,0是空字符的含义,用用到了: ((v ...

  5. 关于javascript:void(0);,herf=”#”以及在IE6下,click事件失效的问题

    经常看到一些网页中,超链接标签中<a href="#"> herf中的"#"改成javascript:void(0);因为#包含了一个位置信息,默认 ...

  6. JavaScript 中 void(0) 的含义

    我想使用过ajax的都常见这样的代码: <a href="javascript:doTest2();void(0);">here</a> 但这儿的void( ...

  7. a href=# 与javascript:void(0)的区别

    跳转到本页面顶部,一般建议写成javascript:void(0);要好一点,点了一点反应都没有,写#点了会跳一下的

  8. href=#与href=javascript:void(0)的区别

    href="#"与href="javascript:void(0)"的区别 # 包含了一个位置信息,默认的锚是#top 也就是网页的上端. 而javascrip ...

  9. a href=#与 a href=javascript:void(0) 的差别

    a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" onCl ...

最新文章

  1. 北京交大计算机学院王浩业,双胞胎双双“吹”进北交大
  2. Unity3d中角色模型和角色名字保持相对位置
  3. iphone全部机型_iPhone12卖爆 产业链喜迎5G时代 股价天花板打开?|iphone|iphone12|运营商|智能机...
  4. linux 7.2中文命令,CentOS7如何支持中文显示
  5. kafka manager 2.0 工具下载 已打包完成
  6. 知道python测试答案_大数据分析的python基础知道章节测试答案
  7. php cli 编程,php-cli下编程如何分层架构、面向对象、统一入口文件?
  8. html阴影 渐变,CSS3:图层阴影及渐变
  9. MultipleRegularExpressionAttribute MVC中扩展自定义验证规则
  10. PHP面向对象学习(一)
  11. 示波器的使用和二极管充放电过程
  12. php获取网页内指定内容,PHP中获取某个网页或文件内容的方法
  13. python元类 orm_Python3 元类与ORM
  14. Vue+Element 表格打印
  15. Ubuntu虚拟机安装
  16. 腾讯裁员,裁出“财务自由”
  17. HDU 5745 La Vie en rose (DP||模拟) 2016杭电多校联合第二场
  18. 亲生骨肉 窥视父母遗产 为了继承遗产竟用这种方式替代...
  19. 关于send和recv在UDP的使用
  20. KVM虚拟机系统迁移

热门文章

  1. MessageBox函数
  2. ASP.NET内置对象二
  3. Visual Studio的Node.js插件:NTVS 1.0正式发布
  4. firefox 插件可能用得上的Firefox插件及下载
  5. 支持Visual Studio 2008和.NET 3.5的企业类库4.0
  6. ASP.NET2.0登陆控件的使用(常见的三种方法)
  7. try except 异常捕获的方法、断言的使用
  8. Zookeeper选举算法原理
  9. BZOJ4832: [Lydsy2017年4月月赛]抵制克苏恩
  10. 在线绘图(PS)(海报)