Search code examples
javascriptcall

How to return from a Javascript funtion without calling return?


I'm trying to simplify a line of code I do use everytime:

if (we_need_to_exit) { op(); return; }

Do I have a chance to define a function, something like this:

function my_return (x) {
  x.op();
  x.return; 
}

and use it like:

if (we_need_to_exit) { my_return(this); }

Is it possible to define such a function?

Edit

Best simple solution that fits in my case is the following:

if (we_need_to_exit) { return op(); }

Solution

  • no, once you call my_return, that return inside of my_return is to return from within my_return.

    You can probably do what you want by:

    if (we_need_to_exit) { return my_fn(this); }
    

    and your my_fn will be:

    function my_fn (x) {
      x.op();
      // return any value you want, or use "return;" or just omit it
    }