Reputation: 43
I need to stop my script. I don't want to write more code like if(...) return false
because I will use it many times.
My English is poor. It's my first question on this site. :)
Upvotes: 3
Views: 1298
Reputation: 4739
In order to exit ALL scopes of your script, you could throw an exception and catch it at the outer scope of your script ;) This imposes, that you adapt your exception handling.
Please note: It is better to write your program in such way, that the following is NOT necessary!
function ExitException() {}
try { //Do your script-stuff here
Foo();
//and do this at any place where you wish to exit:
throw new ExitException;
}
catch (e) {
if (e instanceof ExitException) //This is an Exit. All fine.
console.log("Exit");
else //Pass Exception to the Browser
throw e;
}
Upvotes: 0