JavaScript 中 void 运算符的作用是什么?
我见过有些人在代码中使用 void
运算符。 我还在 href
属性中看到了这一点: javascript:void(0)
似乎并不比 javascript:;
那么,什么使用 void
运算符的理由是什么?
I've seen some people using void
operator in their code. I have also seen this in href
attributes: javascript:void(0)
which doesn't seem any better than javascript:;
So, what is the justification of using the void
operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其使用说明在链接中:
Explanation of its use in links:
直到 JS1.3 之前,
undefined
值在 JavaScript 中无法直接访问。因此包含了一个运算符
void
以允许访问此值。有时,它很有用,特别是在使用 Web API(例如事件处理程序)时,可确保表达式的结果始终
未定义
。当
undefined
属性被添加到 JS1.3 中的全局对象时,void
的实用性变得不明显。因此你的问题。
The
undefined
value was not directly accessible in JavaScript until JS1.3.An operator
void <expression>
was therefore included to permit access to this value.It is sometimes useful, particularly when working with the Web API (e.g. event handlers), to ensure that the result of an expression is consistently
undefined
.When the
undefined
property was added to the global object in JS1.3 the utility ofvoid
became non-obvious.Hence your question.
JavaScript 中,
void
运算符用于显式返回未定义的值。 它是一个一元运算符,这意味着只能使用一个操作数。 您可以如下所示使用它 - 独立使用或与括号一起使用。让我们看一些例子:
如果你问为什么需要一个特殊的关键字来返回 undefined 而不是仅仅返回
undefined
:原因是在 ES5 之前你实际上可以命名一个全局变量undefined< /code>,像这样:
var undefined = "hello"
或var undefined = 23
,大多数浏览器都会接受它; 标识符未定义
并没有承诺实际上是未定义的。 因此,为了返回实际未定义值,使用了void
运算符。 但它并不是一个非常流行的运算符,并且很少使用。让我们看一个带有
void
的函数示例:void
丢弃函数的返回值并显式返回 undefined。您可以从我的教程文章中阅读更多内容: https://josephkhan.me/the-javascript- void-operator/
1 在 ECMAScript 5 及更高版本中,全局变量
undefined
保证是未定义的 (ECMA-262 第五版,§ 15.1.1.3),尽管它是仍然可以将内部作用域内的变量命名为undefined
。The JavaScript, the
void
operator is used to explicitly return the undefined value. It's a unary operator, meaning only one operand can be used with it. You can use it like shown below — standalone or with a parenthesis.Lets see some examples:
If you ask why do you need a special keyword just to return undefined instead of just returning
undefined
: the reason is that before ES5 you could actually name a global variableundefined
, like so:var undefined = "hello"
orvar undefined = 23
, and most browsers would accept it; the identifierundefined
was not promised to actually be undefined¹. So, to return the actual undefined value, thevoid
operator is/was used. It's not a very popular operator though and is seldom used.Let's see an example of function with
void
:void
discards the return value from the function and explicitly returns undefined.You can read more from my tutorial post: https://josephkhan.me/the-javascript-void-operator/
¹ In ECMAScript 5 and later, the global variable
undefined
is guaranteed to be undefined (ECMA-262 5th Ed., § 15.1.1.3), though it is still possible to have a variable inside an inner scope be namedundefined
.请考虑以下事项:
第一个链接将交换文本字段的值。 第二个链接将打开一个新页面,其中包含文本“一条鱼”。 如果您使用
javascript: link
,一旦表达式返回null
或undefined
以外的内容,浏览器就会将其解释为链接的内容应该做。 通过将所有表达式/语句包装在void()
函数中,您可以确保整个代码片段都能运行。 如今,这主要在 Bookmarklet 中使用,因为使用onclick
属性或在单独的 Javascript 块/文件中设置事件处理程序是“规范”。至于
javascript:
与javascript:void()
,第一个语句是不明确的。 你说,“嘿,我想运行一些 javascript”,但你没有提供任何代码。 浏览器在这里应该做什么并不一定很清楚。 对于第二个语句,您说“嘿,运行一些 javascript”,并且您的代码最终返回未定义,浏览器知道这意味着“不执行任何操作”。既然我在这里,我还要指出,使用
javascript:
或javascript:void();
已经不再受到大多数关心标记的人的青睐。 更好的做法是让 onclick 处理程序返回 false,并让链接指向对关闭 javascript 或使用 JavaScript 拦截器(例如 NoScript)的人有意义的页面/资源。Consider the following:
The first link will swap the values of the text fields. The second link will open a new page with the text "one fish". If you use a
javascript: link
, the minute an expression returns something other thannull
orundefined
, the browser will interpret that as what the link should do. By wrapping all expressions/statments in avoid()
function, you ensure your entire snippet of code will run. These days, this is primarily of use in Bookmarklets, as using anonclick
attribute, or setting up event handlers in separate Javascript blocks/files is the "norm".As for
javascript:
vs.javascript:void()
, the first statement is ambiguous. You're saying, "hey, I want to run some javascript", but then you don't provide any code. It's not necessarily clear what the browser should do here. With the second statement you're saying "hey, run some javascript", and your code eventually returns undefined, which the browser knows means "do nothing".Since I'm here, I'll also point out that using either
javascript:
orjavascript:void();
has fallen out of favor with most people who care about markup. The better thing to do is have your onclick handler return false, and have the link pointed towards a page/resource that makes sense for people who have javascript turned off, or are using a javascript blocker such as NoScript.