admin管理员组文章数量:1320860
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.
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.
-
4
+1, interesting. Indeed, why not
void 42
? – Cameron Commented Dec 1, 2012 at 4:50 -
1
Even the link you provided says
void 0
andvoid(0)
are equivalent. – Daniel Miladinov Commented Dec 1, 2012 at 4:51 - unary text operators look weird to a lot of programmers. If it's part of a larger expression though, then the parens control precedence, ya? e.g. void (1+2) – FoolishSeth Commented Dec 1, 2012 at 4:53
- @Cameron that would be an extra byte. – dmnd Commented Dec 1, 2012 at 4:53
-
1
Well then, why not
void 1
orvoid 7
? – Sophie Alpert Commented Dec 1, 2012 at 5:43
3 Answers
Reset to default 6I 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.
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);
Which is identical to:
void 0;
I guess the reason it's being written as a function invocation is because people feel more fortable 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
"So why do people use the parens?"
People do silly things.
"Do they serve a purpose?"
No, they're not needed.
This link explains it for you.
One thing this clarifies is that void is an operator (not a function).Because of this void(0) is technically incorrect though in practice implementations allow it to be used this way it should be used without parentheses e.g. void 0.
So its technically wrong to use void(0)
but in practise void has two different syntaxes:
void (expression)
void expression
MDN already tells you that, though no explicit statement has been made regarding the two syntaxes, as its not technically correct.
Courtesy:
- http://www.coderenaissance./2011/02/javascripts-void-operator-explained.html
- http://www.w3resource./javascript/operators/void.php
- https://developer.mozilla/en-US/docs/JavaScript/Reference/Operators/void
本文标签:
版权声明:本文标题:Many people write `javascript:void(0)` instead of `javascript:void 0` in hrefs. Do the parentheses do anything? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742087540a2420071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论