Sometimes you are doing things right w/o analyzing why it's right, you just
feel that. All these years I followed the naming rule I spelled in the
original posting, no matter what language I was working with. And as a
reward I never had any problems with identifiers' names. (I had a bunch of
other problems of course
This discussion got me so exited that I took some books from the dustiest
corner of my shelve.
So...
JavaScript and JScript are both built on ECMA-262 language specifications,
approved as international standard ISO/IEC 16262. The official name for both
languages is ECMAScript (in the standardization papers).
The latest ECMA-262 specs can be obtained at
www.ecma.ch
ECMA-262, Version 3 Final, paragraph 7.6:
Identifiers are interpreted according to the grammar given in Section 5.16
of the upcoming version 3.0 of the Unicode standard, with some small
modifications. This grammar is based on both normative and informative
character categories specified by the Unicode standard. The characters in
the specified categories in version 2.1 of the Unicode standard must be
treated as in those categories by all conforming ECMAScript implementations;
however, conforming ECMAScript implementations may allow additional legal
identifier characters based on the category assignment from later versions
of Unicode.
This standard specifies one departure from the grammar given in the Unicode
standard: The dollar sign ($) and the underscore (_) are permitted anywhere
in an identifier. The dollar sign is intended for use only in mechanically
generated code.
Unicode escape sequences are also permitted in identifiers, where they
contribute a single character to the identifier, as computed by the CV of
the UnicodeEscapeSequence (see section 7.8.4). The \ preceding the
UnicodeEscapeSequence does not contribute a character to the identifier. A
UnicodeEscapeSequence cannot be used to put a character into an identifier
that would otherwise be illegal. In other words, if a \
UnicodeEscapeSequence sequence were replaced by its UnicodeEscapeSequence's
CV, the result must still be a valid Identifier that has the exact same
sequence of characters as the original Identifier.
Two identifiers that are canonically equivalent according to the Unicode
standard are not equal unless they are represented by the exact same
sequence of code points (in other words, conforming ECMAScript
implementations are only required to do bitwise comparison on identifiers).
The intent is that the incoming source text has been converted to normalised
form C before it reaches the compiler.
Conclusion:
"1", "something[0]" and similar are not valid identifiers and cannot be used
for naming of variables, objects, methods and properties within the script.
Even if a particular browser allows the use of such identifiers in violation
of the ISO standards, programmer should avoid them to keep his/her code
compatible with other platforms.
Lasse Reichstein Nielsen said:
VK said:
The naming confusion stays on the fact that smart guys in 3W have forgotten
of the dual nature of named page elements (forms, form elements, divs etc.)
If browser supports JavaScript, each named element is being AUTOMATICALLY
added to the corresponding JavaScript array (document.forms[],
document.forms[x].elements[] etc.) on page load.
Maybe they haven't forgotten it. Maybe they never considered it.
Javascript, or other script languages (and there are plenty that *can*
be embedded in a web page), are separate from HTML. They need not, and
should not, affect the specification of HTML (IMNSHO).
So each name is not just a name, but a potential JavaScript variable.
Not a variable. A property. That is a *big* difference, since any
string can be used as a property name.
So it has to follow the JavaScript naming scheme. Otherwise
JavaScript fails to register the element in an array, which
invalidates the element for scripting.
Not at all.
This works in Mozilla and Opera (and fails in IE6 even if the name is
as simple as "aa").
---
<a name="DX üt" href="test.html">FooM</a>
<script type="text/javascript">
alert(document.links["DX üt"].href);
</script>
---
The W3C-DOM method version works in both of these and in IE6:
---
<a name="DX üt" href="test.html">FooM</a>
<script type="text/javascript">
alert(document.links.namedItem("DX üt").href);
</script>
---
IE apparently doesn't follow the W3C DOM 2 HTML recommendation (but we
knew that).
I have no time for further research, but I bet my coffee against your sugar:
If you disallow JavaScript in your browser, it will eat CDATA names with no
problem.
It will with Javascript too.
(And yes, until eariler today, I though "foo[2]" was an illegal name
attribute value in HTML, but it seems it is not.)
/L