J
John G Harris
Dr J R said:In comp.lang.javascript message
Perhaps. But first you have to tell me what they, in your opinion,
are, and I have to agree with you or at least believe you.
Here is what I would have written. You'll notice that some of my
assertions differ from some of yours.
===========================================================
Identifiers
Identifiers give names to variables, functions, and
properties so that the compiler knows which one you're
talking about.
With some restrictions, an identifier is any combination of
letters, digits, '_', and '$' characters. Unicode letters
and a few obscure Unicode symbols are allowed. Identifiers
are case sensitive.
One restriction is that an identifier does not start with a
decimal digit, ('0' to '9'). The other restriction is that
an identifier is not one of the javascript keywords, (while,
var, in, ...), nor one of the words reserved for future use,
(class, goto, ...), nor one of the words null, true, false.
It is best not to use '$' as it makes identifiers difficult
to read and is there for use by programs that write
javascript programs.
Variables
It is better to declare each variable explicitly, as in
var x;
If this is done inside a function then x becomes a local
variable, different from any global variable and any
local variable named x in other functions. It is also
different from the local variable named x in a recursive
call of the same function.
===========================================================
Semicolons
Some statements end with a semicolon, ';'; some end with a
close curly brackets, '}'; all other statements end with a
nested, simpler, statement. Here is an example that shows
all three cases :
if (x==0)
{ y = 0; z = 1; }
The 'if' statement ends with a block statement, { ... }.
The block statement ends with a close curly brackets. Each
of the two statements inside the block statement ends with
a semicolon.
A 'var' declaration is classified as a statement. It ends
with a semicolon.
A 'function' declaration is not classified as a statement,
though it looks much the same. It ends with a close curly
brackets.
In some cases you need not write the semicolons yourself.
In effect, the compiler will write them for you. One case
is when the semicolon is at the end of the line *and* the
beginning of the next line cannot possibly be part of the
same statement. The other case is when the semicolon is
followed by a close curly brackets. (Ignore comments when
applying these rules). Thus the example can be written as :
if (x==0)
{ y = 0
z = 1 }
In return for being able to leave out some of the
semicolons you must be careful where you put your line
endings. In particular, if a function returns a value then
the expression giving the value must start on the same line
as the 'return' keyword.
The semicolons inside a 'for' condition, (... ; ... ; ...),
cannot be omitted.
The full rules are given in ECMA 262.
===========================================================
John