Webdialogs and Javascript void
-
I have spend a serious amount of time figuring out why some of my buttons in javascript did not work.
It was due to the void(0) parameter which can be set.Example:
<a href="JavaScript:void(0);" ondblclick="alert('Well done!')">Double Click Me!</a>Hope this helps anyone.
Pout
-
What does
void(0)
do? -
*void
In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined. JSLint does not expect to see void because it is confusing and not very useful.*
-
The result of this is the same as returning false in the event to prevent the browser from further processing the event?
-
When I saw Mr J S Lint didn't care for it I investigated no further.
-
True, but some sites refer to it as usable in JS (which it is in fact)
Example:
http://www.tizag.com/javascriptT/javascriptvoid.php -
@pout said:
True, but some sites refer to it as usable in JS (which it is in fact)
Example:
http://www.tizag.com/javascriptT/javascriptvoid.phpDoes
void
differ fromreturn null
? -
@dan rathbun said:
@martinrinehart said:
Does
void
differ fromreturn null
?YES. There's a gotcha (because of backward compatibility.)
%(#BF00BF)[(object.prop==null)]
will return true if the poperty has the value null or if the property does not exist!Dan, I enjoyed your very deep response. Thank you!
But my question was really about the original proposed use. I get the intended result with
<a href='javascript:return null' ondblclick='...'>doubleclick-only link!</a>
, without offending Doug Crockford.And do you agree with Crockford? "In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined. JSLint does not expect to see void because it is confusing and not very useful." (Why "prefix operator"?)
-
@martinrinehart said:
Dan, I enjoyed your very deep response. Thank you!
But my question was really about the original proposed use. I get the intended result with:
%(#804000)[<a href=']%(#8000BF)[javascript:return null]%(#804000)[' ondblclick=']%(#8000BF)[...]%(#804000)['>doubleclick-only link!</a>]
without offending Doug Crockford.
(1) Not in my opinion,... you are 'misusing' return in the same way that offends Doug regarding the misuse of void.
And that is, misusing the function (or operator,) by forcing the processing of a meaningless literal expression such as 0, (ie: void(0);) when it is preferred for clarity, and faster for processing, to simply use return; (as return with no expression returns undefined.)
Doug also seems to imply that the choice of the word 'void' for the Js "return_undefined_after_eval" function name, is confusing because of it's accepted use in other languages. I give him that. The inventer of Js could have named it the noreturn operator.
(2) As mentioned above, there can be compatibility problems with the use of null. (And, once again, why force the machine to process the null expression if it is not necessary?)@martinrinehart said:
And do you agree with Crockford? **> @unknownuser said:
**.
**In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined."
**[Dan inserts: 2 statements of fact - agreement not necessary.] **> @unknownuser said:**.
JSLint does not expect to see void because it is confusing and not very useful.Two opinions:
(1) "confusing" - The void operator, is only confusing when it's misused, (as stated above.)
(2) "usefulness" - The void operator was impletemented for a real purpose. When the expression to be evaluated, is a valid expression (processing work needs to be done to evaluate it, and that evaluation is necessary;) but the results need to be prevented from returning to the caller.
The perfect exampleis the problem Todd was having with Js callbacks to a Sketchup Ruby. In that situation, the proper use of void is a valid answer to the problem.
See SCF topic: Anyone seen this web dialog bug?@martinrinehart said:
Why "prefix operator"?
A prefix operator is one that is stated before an expression (ie: it operates on the expression that follows itself.)
In Ruby, we have some "double-duty" reserved words that can also be used as prefix operators. Examples are if and unless. (Although the Ruby manual calls them 'modifiers' when they are used in this way.)
( *expression that does something* ) **if** ( *boolean expression* )
or
( *expression that does something* ) **unless** ( *boolean expression* )
-
@martinrinehart said:
What does
void(0)
do?The void operator evaluates the given expression and then returns undefined. If you have pass 0 as the unary expression operand to the void operator, JavaScript coerces 0 to "false" and returns, but void doesn't care and simply returns undefined, which means "do nothing" . Put them together and you have composed a way to programmatically "do nothing" when a link is clicked. JavaScript Void(0) is often used when, inserting an expression into a web page may produce an unwanted side-effect.
-
@martinrinehart said:
Does
void
differ fromreturn null
?YES. (in the following, violet denotes javascript.)
(1) return type: undefined and null are separate return types.
___http://msdn.microsoft.com/en-us/library/7wkd9z69(VS.85).aspx- void(whatever) always returns undefined* return( null ) obviously will return null (in this case.)A variable can have a null value, but not an undefined value, because undefined refers to the object, not it's value. You check undefined using quotes:
%(#BF00BF)[if(typeof(var)=="undefined") //do something]
You check null without quotes:
%(#BF00BF)[if(var==null) //do something]
The difference is subtle. Basically, if a symbol references a variable that has not been declared, or has been declared but never had a value assigned, then "undefined" is returned. If a symbol references a variable that has had any value (including null,) assigned to it, it will not return "undefined".
But be careful! There's a gotcha (because of backward compatibility.)
%(#BF00BF)[(object.prop==null)]
will return true if the property has the value null or if the property does not exist! You can use in to be more specific:
%(#BF00BF)[if ("prop" in object)]
will return true if the property does exist.
(2) Both are evaluation functions:
- return( expression ) [called a Statement] Always returns the result (of whatever type,) that expression evaluates to.
http://msdn.microsoft.com/en-us/library/22a685h9(VS.85).aspx - void( expression ) [called an Operator] Evaluates expression then always returns "undefined" (regardless of the result.) The expression is required or an argument error would be raised. The expression 0 is traditional, and comes from C language (where null and zero were interchangable; but in javascript, 0 is a number type, null is it's own type.)
http://msdn.microsoft.com/en-us/library/e17c7cbe(VS.85).aspx
The internal code might be represented as:
%(#BF00BF)[function void( *expression* ) {] %(#F0F0F0)[___]%(#BF00BF)[eval( *expression* );] %(#F0F0F0)[___]%(#BF00BF)[return( undefined ); }]
___
- void(whatever) always returns undefined* return( null ) obviously will return null (in this case.)A variable can have a null value, but not an undefined value, because undefined refers to the object, not it's value. You check undefined using quotes:
Advertisement