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.

Share Improve this question asked Dec 1, 2012 at 4:47 dmnddmnd 2,4912 gold badges22 silver badges28 bronze badges 5
  • 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 and void(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 or void 7? – Sophie Alpert Commented Dec 1, 2012 at 5:43
Add a ment  | 

3 Answers 3

Reset to default 6

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.

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

本文标签: