Re: Javascript Best Practices Document v1.0



On 11/10/2005 04:06, Matt Kruse wrote:

http://www.JavascriptToolbox.com/bestpractices/

"Square Bracket Notation"

You might want to note the different restrictions that both forms have. That is, the token that forms each part of the dot notation property accessor must conform to the Identifier production, whereas property names used with square brackets have no such restrictions. However, the expression within the brackets will /always/ be type-converted to a string.

This might also be a good time to follow on and dispel the idea about hash 'arrays'.

I don't know about always using square bracket notation.


"Referencing Forms And Form Elements"

Further to Gérard's complaint, it might be better to refer abstractly to form identifiers (as both the name and id attributes are supposed to be unique identifiers), unless the author is referring to the form through an event listener in which case no identifier is needed.


"Problems With Concatenation"

  +theform.elements["val1"].value + +theform.elements["val2"].value

I think it's better to use parentheses around the unary expression, especially with the right-hand operand above.

It might be better to reference the elements collection directly, rather than the form (at least when you aren't dealing with the form itself).

  var controls = document.forms.myForm.elements,
      total    = +controls.val1.value + (+controls.val2.value);


"Getting And Setting Form Values"

  "[Using unified functions for] forms with only 1 radio button
   in a group [...]"

A singular radio button should never exist. If the document is generated server-side (surely the only reason this situation could occur), that form control should be removed and the served script altered to only deal with a single control.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.