为什么Javascript中的void需要参数? [英] Why does void in Javascript require an argument?

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

问题描述

根据我的理解,Javascript中的关键字 void 是某种函数,它接受一个参数并始终返回 undefined 价值。出于某种原因,你需要传递一个参数;如果没有它,它将无法运作。

From what I understand, the keyword void in Javascript is some kind of function that takes one argument and always returns the undefined value. For some reason you need to pass it an argument; it won't work without one.

它有什么理由需要这个论点吗?

Is there any reason why it requires this argument?

有什么意义?为什么没有争论就行不通。我见过的唯一用途是生成 undefined 结果。它还有其他用途吗?

What is the point? Why won't it work without an argument. The only use I have seen for it is to produce an undefined result. Are there any other uses for it?

如果没有,那么表达式的要求似乎没有意义。

If not then it would seem that the requirement for an expression to be passed would be pointless.

推荐答案

根据此页 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void void是一个运算符,只返回 undefined ,在评估传递给它的表达式之后。操作员需要操作数才能操作。这就是传递参数的原因。

As per this page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void void is an operator, which simply returns undefined, after evaluating the expression you pass to it. An operator needs an operand to operate on. That is why pass a parameter.

console.log(void true);
console.log(void 0);
console.log(void "Welcome");
console.log(void(true));
console.log(void(0));
console.log(void("Welcome"));

所有这些陈述都会打印 undefined

All these statements would print undefined

var a = 1, b = 2;
void(a = a + b)
console.log(a);

这将打印 3 。因此,很明显,它会评估我们传递给它的表达式。

And this would print 3. So, it is evident that, it evaluates the expressions we pass to it.

编辑:我从这个答案中学到了https://stackoverflow.com/a/7452352/1903116

As I learn from this answer https://stackoverflow.com/a/7452352/1903116

undefined 只是一个可以写入的全局属性。例如,

undefined is just a global property which can be written to. For example,

console.log(undefined);
var undefined = 1;
console.log(undefined);

打印

undefined
1

所以,如果你想绝对确保使用 undefined ,您可以使用 void 运算符。由于它是一个运算符,因此无法在javascript中覆盖它。

So, if you want to absolutely make sure that the undefined is used, you can use void operator. As it is an operator, it cannot be overridden in javascript.

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

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