[thelist] DHTML questions...

.jeff jeff at members.evolt.org
Fri Nov 16 01:57:34 CST 2001


> From: The Optimizer
> > function locateBox(top_loc,left_loc)
> >     {
> >     return top_loc;
> >     return left_loc;
> >     }
> First point, you need to understand variable scope.
> These are local variables, their scope being within
> the function only.

actually they're neither local or global.  they're statements that are being
returned to the calling object or event handler.  technically only the first
one is being returned because execution of a return statement halts further
execution of the function.

i'll describe briefly how variable scoping works in javascript so everyone
understands.  when creating variables they are global by default, unless you
specify otherwise.  in order to create a local variable, you must use the
var keyword within the function.

think of a function like a box with holes in its walls for every variable
inside and outside the function.  now, every time you refer to these
variables either to read or write them, it reaches outside the box through
one of these holes to access them.  if you define a variable inside the
function as a local variable (using the var keyword), it doesn't create a
hole for it to the outside or if a variable by the same name already exists
outside the function it closes the hole off for that variable.  this has a
couple of side-effects.

1)  if there _is not_ a variable by the same name
    defined outside the function, then trying to
    access a variable by that name outside the
    function will result in an error.

here's an example:

<script language="JavaScript" type="text/javascript">
  function myFunc()
    var myVar = 'foo';

// -->

run this function and you'll get an error "myVar is undefined".

2)  if there _is_ a variable by the same name
    defined outside the function, then setting the
    value of the variable within the function will
    not change the value of the variable outside
    the function.  in fact, it's impossible to read
    or write the variable outside the function as
    defining the variable of the same name within
    the function makes the variable outside the
    function invisible.

<script language="JavaScript" type="text/javascript">
  var myVar = 'foo';

  function myFunc()
    var myVar = 'bar';



// -->

the 1st alert dialog will contain the value "foo".  the 2nd (the 1st one
inside the function "myFunc()") will contain the value "foo" because the
local variable has not been defined yet so the function can access the
global variable.  the 3rd (the 2nd one inside the function "myFunc()") will
contain a value of "bar" because the defining of the local variable
overwrites the global variable within the function.  the 4th and final one
will contain the value "foo" once again as the global variable value still
has not been changed.

i hope this makes variable scoping alittle more clear for someone.



jeff at members.evolt.org

More information about the thelist mailing list