R
Richard Cornford
Toby Inkster wrote:
In javascript it is not strictly necessary to declare global variables
in the global scoop. The act of assigning a value to an otherwise
undeclared Identifier will result in the creation of a named property of
the global object, and the assignment of the value to that property. As
global variables are all named properties of the global object the only
practical difference is that with a global declaration the global object
has the named property form variable institution onward (but it has an
undefined value until the assignment) while direct assignment to an
undeclared Identifier does not create the named property until the
assignment. That difference is only significant if the code is going to
attempt to read from the global property before it exists, which
probably makes no real difference as reading from the property before it
is assigned a value would be equally error-prone in non-defensive code.
Still, the recommendation would be to declare global variables in the
global scope regardless, as that makes it obvious that Identifiers that
refer to those variables are supposed to be global, rather than
variables that are erroneously leaking form their intended scope.
Except that if fritz's documents ever actually get to be interpreted as
XHTML it would be the namespace qualified version -
getElementsByTagNameNS - that would be used in the XHTML DOM.
Richard.
... . You'd have to redefine "m" in each function. This is
because of variable scoping -- "m" only has a meaning in
the function where it is originally declared. Of course you
can get around this by initially declaring "m" outside of
all the functions.
e.g.
=============== script.js ===============
var m; //declare "m"
<snip>function init () {
// "m" has meaning here
m = document.getElementById("menu");
In javascript it is not strictly necessary to declare global variables
in the global scoop. The act of assigning a value to an otherwise
undeclared Identifier will result in the creation of a named property of
the global object, and the assignment of the value to that property. As
global variables are all named properties of the global object the only
practical difference is that with a global declaration the global object
has the named property form variable institution onward (but it has an
undefined value until the assignment) while direct assignment to an
undeclared Identifier does not create the named property until the
assignment. That difference is only significant if the code is going to
attempt to read from the global property before it exists, which
probably makes no real difference as reading from the property before it
is assigned a value would be equally error-prone in non-defensive code.
Still, the recommendation would be to declare global variables in the
global scope regardless, as that makes it obvious that Identifiers that
refer to those variables are supposed to be global, rather than
variables that are erroneously leaking form their intended scope.
<snip>getElementById is a useful method, but getElementsByTagName
is also very handy.
Except that if fritz's documents ever actually get to be interpreted as
XHTML it would be the namespace qualified version -
getElementsByTagNameNS - that would be used in the XHTML DOM.
Richard.