[thelist] RE: DHTML questions...

Ben Gustafson ben_gustafson at lionbridge.com
Thu Nov 15 15:45:00 CST 2001


<snip>
> > > 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.
>
> So?

*sigh* people are awfully quick to argue on this list. You can hardly
dispute the need for comprehension of the difference between global and
local variables in JavaScript if one wishes to know how to use it
effectively.

> Their values are returned, not the variables themselves.

No they aren't. Hence the need to understand variable scope, and the
manner
in which to use global and local variables. A function can only return
one
value, thus only the first value is returned. If global variables had
been
set in the function, then both values would have been available.
</snip>

I think the unstated problem here is to what the variables are returned
by the function. You can't return more than one value to a variable,
unless it's an object. This code snippet shows a way to do so:

var getTheLoc = new Object();

getTheLoc.top_loc = 10;
getTheLoc.left_loc = 20;

alert("top_loc: " + getTheLoc.top_loc + " left_loc: " +
getTheLoc.left_loc);

function locateBox(top_loc, left_loc) {
 var theLoc = new Object();
 theLoc.top_loc = top_loc;
 theLoc.left_loc = left_loc;
 return theLoc;
}

getTheLoc = locateBox(40,60);

alert("top_loc: " + getTheLoc.top_loc + " left_loc: " +
getTheLoc.left_loc);

--Ben Gustafson
  Lionbridge Technologies, Inc.
  www.lionbridge.com





More information about the thelist mailing list