Z
Zack
Hi,
Let's say you have this function named validate. validate looks like
this:
function validate() {
return ($F("Email") != '' ? ($("Name").value = $F("Email")) :
false);
}
(Using prototype.js. For those not familiar $() is like getElementById
and $F is like getElementById().value for form elements) There's a lot
happening on this 1 line so let's break it down.
If the value of element "Email" is not empty string, return the result
of setting value of element "Name" = the value of element "Email",
otherwise return false.
This works exactly as I want it too, but is it OK? By OK I mean good
javascript style? There's something fishy smelling to me about doing
any sort of assignment in the expression part of the conditional. It's
kind of cool that it all works because ($("Name").value =
$F("Email")) returns a truthy value when the value of "Email" is not
falsy
If anyone can give me some expert input that would be wonderful. Smack
this idea out of my head, or caution me it may lead to other
unintended side effects, or tell me yes, this is awesome and I'm a
ninja! I've checked around this group and Douglas Crockford's site,
but haven't found anything definitive.
The safer, more standard way of writing this, to take the assignment
out of the expression would be
function validate() {
$("Name").value = $F("Email");
return ( ($F("Email") != '') ? true : false);
}
But it's just not as cool looking. What does everyone think?
Let's say you have this function named validate. validate looks like
this:
function validate() {
return ($F("Email") != '' ? ($("Name").value = $F("Email")) :
false);
}
(Using prototype.js. For those not familiar $() is like getElementById
and $F is like getElementById().value for form elements) There's a lot
happening on this 1 line so let's break it down.
If the value of element "Email" is not empty string, return the result
of setting value of element "Name" = the value of element "Email",
otherwise return false.
This works exactly as I want it too, but is it OK? By OK I mean good
javascript style? There's something fishy smelling to me about doing
any sort of assignment in the expression part of the conditional. It's
kind of cool that it all works because ($("Name").value =
$F("Email")) returns a truthy value when the value of "Email" is not
falsy
If anyone can give me some expert input that would be wonderful. Smack
this idea out of my head, or caution me it may lead to other
unintended side effects, or tell me yes, this is awesome and I'm a
ninja! I've checked around this group and Douglas Crockford's site,
but haven't found anything definitive.
The safer, more standard way of writing this, to take the assignment
out of the expression would be
function validate() {
$("Name").value = $F("Email");
return ( ($F("Email") != '') ? true : false);
}
But it's just not as cool looking. What does everyone think?