在Javascript中,是否可以从内部函数停止外部函数的执行?

cal*_*oop 5 javascript

是否有可能将外部函数从其内部函数中分离出来?

(function one(){
    var foo = (function two(){
        if(true){
            return true; //all good
        }else{
            //how can we break function one here?
    })();
    //extra statements only executed if foo is true
})();
Run Code Online (Sandbox Code Playgroud)

没有所谓的“退货”吧?:P

更新

谢谢大家 - 因为我怀疑我正在寻找的“退货”功能是不可能的。但是根据您的输入/提示,我重新编写了代码并使用了 try/catch 块。在 JS 中给猫剥皮的方法有很多种!

Rok*_*jan 5

return仅适用于它所使用的函数内部。

你能做的就是two()返回一个布尔值。
比two()在语句内if评估one()

function two() {

  // Some logic here...
  var isError = true; // sometimes is false :)
  
  // Internals
  if (isError) {
    console.log("Something is wrong");
  }
  
  // Finally return a boolean
  return isError;
}


(function one(){

  // `two()` returns a boolean, true if there's an error.
  // `return` will stop further execution of `one()` 
  if( two() ) return; 
  
  console.log("This will not log if there was an error")

})();
Run Code Online (Sandbox Code Playgroud)