为什么要使用void关键字? [英] Why use the void keyword?

查看:93
本文介绍了为什么要使用void关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 kriskowal / q 中看到了这个片段:

I came across this snippet in kriskowal/q:

/**
 * Applies the promised function in a future turn.
 * @param object    promise or immediate reference for target function
 * @param args      array of application arguments
 */
Q.fapply = fapply;
function fapply(value, args) {
    return dispatch(value, "apply", [void 0, args]);
}

使用无效关键字?为什么不写 [undefined,args]

What is the point of using the void keyword? Why not just write [undefined, args]?

推荐答案

MDN无效文档


语法

void expression

使用

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

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

虚空运算符通常仅用于获取未定义的
原始值
,通常使用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).

为什么?请参阅 MDN未定义

在旧版本的JavaScript undefined 中可以被覆盖,但是从JavaScript 1.8开始.5,undefined是不可写的,符合ECMAScript 5规范。

In older versions of JavaScript undefined could be overridden, but in starting in JavaScript 1.8.5, undefined is non-writable, as per the ECMAScript 5 specification.

这篇关于为什么要使用void关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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