JavaScript中的void运算符有什么意义? [英] What is the point of void operator in JavaScript?

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

问题描述

我见过一些人在他们的代码中使用 void 运算符。我也在 href 属性中看到了这一点: javascript:void(0)这似乎不比 javascript:;

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:;

那么,使用 void 运营商?

推荐答案

在链接中使用的说明

这就是bookmarklet
经常将代码包装在void()或
中的原因,一个不是
的匿名函数返回任何东西来阻止浏览器
试图显示执行bookmarklet的
的结果。对于
示例:

This is the reason that bookmarklets often wrap the code inside void() or an anonymous function that doesn't return anything to stop the browser from trying to display the result of executing the bookmarklet. For example:

javascript:void(window.open("dom_spy.html"))

如果您直接使用返回
的代码(这种情况下
中的新窗口实例) ,浏览器最终将显示
,显示:

If you directly use code that returns something (a new window instance in this case), the browser will end up displaying that:

javascript:window.open("dom_spy.html");

在Firefox中,上面会显示:

In Firefox the above will display:

[object Window]


这篇关于JavaScript中的void运算符有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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