许多人在hrefs中写成javascript:void(0)而不是javascript:void 0.括号有什么作用吗? [英] Many people write `javascript:void(0)` instead of `javascript:void 0` in hrefs. Do the parentheses do anything?

查看:55
本文介绍了许多人在hrefs中写成javascript:void(0)而不是javascript:void 0.括号有什么作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到人们一直在写 void(0),但是我不明白为什么人们使用括号.据我所知,它们没有目的. void 不是函数,它是一个运算符.那么人们为什么要使用括号呢?他们有目的吗?即使在MDN上也使用了括号.

I see people write void(0) all the time, but I don't understand why people use parentheses. As far as I can tell, they have no purpose. void is not a function, it's an operator. So why do people use the parens? Do they serve a purpose? Even on MDN the parens are used.

推荐答案

我不得不承认我过去多次使用相同的构造,这主要是因为我已经看到它在其他站点上得到了使用.我不再使用它,因为与内联JavaScript相比,首选非干扰性JavaScript.实际上,它几乎专门用于内联,以确保页面不会刷新.

I have to admit that I've used that same construct many times in the past, mainly because I've seen it being used on other sites. I'm no longer using this because unobtrusive JavaScript is preferred over inline JavaScript; in fact, it's almost exclusively used inline to make sure the page doesn't refresh.

正如您正确指出的那样,它是运算符而不是函数;之所以仍然有效的原因仅仅是因为(0) 0 是同一件事,所以这是它的评估方式:

Having said that, as you have rightfully pointed out, it's an operator and not a function; the reason it still works is simply because (0) and 0 are the same thing, so this is how it would be evaluated:

void (0);

与以下相同:

void 0;

我猜它之所以被编写为函数调用的原因是因为人们在内联使用时对函数感到更自在:)

I guess the reason it's being written as a function invocation is because people feel more comfortable with functions when used inline :)

<a href="javascript:void 0">...</a> // hold on, that can't work, can it?!

<a href="javascript:void(0)">...</a> // ahhh, normality restored

这篇关于许多人在hrefs中写成javascript:void(0)而不是javascript:void 0.括号有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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