getting a mouseover on a 'div'

R

Richard Cornford

Toby Inkster wrote:
... . 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"
function init () {
// "m" has meaning here
m = document.getElementById("menu");
<snip>

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.

getElementById is a useful method, but getElementsByTagName
is also very handy.
<snip>

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.
 
F

Fritz

Richard said:
Toby Inkster wrote:




<snip>

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>

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.
Thank you Richard,
In other words, don't make a variable global by accident (defining
and assigning at the same time) but use good code practices and define
the globals where they should be. I didn't realize that in javascript
that the assignment of a value at the same time as it's first mention
inside a function would ooze it out into the global space. I have the
feeling that a lot of others didn't know it either.
Actually I had planned to have one sheet strictly for globals while
developing the site and if there were enough of them at the end, then
leave it rather than transferring them to an all encompassing one.

Thanks again.
f.
 
F

fritz

Perhaps I could ask a quick javascript question here?

if x == 0
and y == 10
I can say:
x=y+1
and now x == 11

can I also say?
x = y++
and now x still == 10 and
x = ++y
and now x == 11?


The ++ and -- are mentioned in my book but there are no examples of the
use of it. If I remember, someone said that javascript took a lot from
C. Well, in a time long ago and a place far, far, away, I remember that
in C one could either use the value and then have it incremented or use
the value which had already been incremented.

Probably I should just try it out.

f.
 
F

fritz

fritz said:
Perhaps I could ask a quick javascript question here?

if x == 0
and y == 10
I can say:
x=y+1
and now x == 11

can I also say?
x = y++
and now x still == 10 and
x = ++y
and now x == 11?


The ++ and -- are mentioned in my book but there are no examples of the
use of it. If I remember, someone said that javascript took a lot from
C. Well, in a time long ago and a place far, far, away, I remember that
in C one could either use the value and then have it incremented or use
the value which had already been incremented.

Probably I should just try it out.

f.
I did and there is no need to comment. It works.
Find out for one's self prior to asking for help!

f.
 
N

Noozer

fritz said:
Perhaps I could ask a quick javascript question here?

if x == 0
and y == 10
I can say:
x=y+1
and now x == 11

After this X=11 and Y=10

can I also say?
x = y++
and now x still == 10 and
x = ++y
and now x == 11?

After this X=11 and Y=11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top