[thelist] "return" in JS ... continued

Tom Dell'Aringa pixelmech at yahoo.com
Tue Jun 4 16:07:07 CDT 2002


Eric,

Ok, again that makes sense and I see why you would do it. Now hopefully this is my last question
on this, but this script really got me thinking about all this. Here is one more snippet, this is
part of a larger onMouseDown script:

if (isActive == true)
{
 //are we on the knob?
 if ((x >= dragObject.xpos) && (x <= dragObject.xpos + dragObject.w))
 {
   //where 'x' is on the knob
   dragObject.dragOffsetX = x - dragObject.xpos;
 }
 return false;
}
 else
{
 return true;
}

Now, I can see on the return false - make sure you are pulling out of the function like you said
before the else and the last few brackets. But here, the author of this script (not me) seems to
be returning either a true or false value depending on whether a drag object was selected. The
caller of this script is document.onmousedown. Why would this event need a true or false value? It
seems on the surface it would not, and commenting out these lines seems to have no effect on the
drag working at all.

I would just take it out but I feel I might be missing something..and I do have an occasional
glitch in the drag where you get the black "stop" icon, it seems like the mousedown gets stuck now
and then. Not only do I want to understand return better, but i want to see if it is affecting
this script.

Tom

--- "Means, Eric D" <eric.d.means at boeing.com> wrote:
> >  -----Original Message-----
> > From: 	Tom Dell'Aringa [mailto:pixelmech at yahoo.com]
> > Sent:	Tuesday, June 04, 2002 3:43 PM
> > To:	thelist at lists.evolt.org
> > Subject:	RE: [thelist] "return" in JS question
> >
> > So the return statement in this function is useless then, wouldn't it be?
> It's not even sending a
> > value so it would be undefined..
> >
> > function mouseUp(e) {
> >  var x = event.x + document.body.scrollLeft;
> >  var y = event.y + document.body.scrollTop;
> >  isActive = false;
> >  return;
> > }
>
> Basically.  The closing brace implies a return statement, so the return; is
> redundant.  It's often put in for one of a couple of reasons:
> 1. Clarity.  It can be easier to spot a return; than it can be to spot which
> of a series of closing braces belongs to the function.
> 2. Exiting the function from nested braces:
> function whatever () {
>   if (something) {
>     ...
>     if (something_else) {
>
>     } else {
>       // something is wrong, exit the function
>       return;
>     }
>   }
> }
> 3. Having a final return statement can make sure that you *know* what the
> function will return if it manages to get all the way to the final brace; in
> this case it almost always has an argument, however (return true, return 0,
> etc).
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com



More information about the thelist mailing list