简体   繁体   English

如何终止JavaScript中的父函数执行?

[英]how to terminate parent function execution in javascript?

I need to override some behaviour of function, that goes after it calls other function. 我需要重写函数的某些行为,即它调用其他函数之后的行为。 The problem is that this parent function is a library and I dont want to change it, so solutions like make some flag or another change of this function is not so good. 问题在于该父函数是一个库,我不想更改它,因此诸如对该函数进行一些标记或其他更改的解决方案不是很好。 I know that I got an caller object in function that I can change, so maybe I can figure out smth with it. 我知道我在函数中有一个可以更改的调用方对象,所以也许我可以弄清楚它的含义。 Heres the example: 这是示例:

function parent()
{
  console.log("some need stuff");
  some.handler.function.from.config.that.i.can.change();
  console.log("need omit this right till the end");
}

function child()
{
  console.log("need to somehow stop evaluation of " + child.caller + " function");
}

As a ruby programmer I know there is lambdas with which you can terminate evaluation from inner scope of closure. 作为红宝石程序员,我知道有lambda,您可以使用它们终止内部封闭范围内的评估。 But Im not sure how to do this from javascript. 但是我不确定如何从javascript做到这一点。

You can't do that directly. 您不能直接这样做。 (Moreover .caller is obsolete) (此外, .caller已过时)

You can however use a dirty trick: 但是,您可以使用肮脏的把戏:

try{
    parentFunction();//calls child
}catch(e){
   //done
}

function child(){
    doWhatever();
    throw new Error("this will hopefully propagate");
}

Fiddle 小提琴

This will only work assuming the parent does not catch exceptions itself when calling the child. 这仅在假设父级在调用子级时本身未捕获异常的情况下才有效。

Moreover, it is generally a bad idea to use exceptions for flow control. 而且,使用异常进行流控制通常是一个坏主意。 Use this as a last resort. 将此作为最后的手段。

Call a new function that you do control before you call the library, and wrap the call to the library you cannot modify in a try/catch block. 在调用库之前,请先调用一个您要控制的新函数,然后将调用包装到您不能在try / catch块中修改的库。

For example: 例如:

function meta-parent()
{
  try {
    parent();
  }
  catch (e){
      // don't really be empty!
  }
}

function parent()
{
  console.log("some need stuff");
  some.handler.function.from.config.that.i.can.change();
  // Nothing below here will run because of your child's exception
  console.log("need omit this right till the end");  
}

function child()
{
  console.log("need to somehow stop evaluation of " + child.caller + " function");
  throw new Error(); //breakout!
}

Note inspiration: Breaking a parent function from within a child function (PHP Preferrably) 注意灵感: 从子函数中断开父函数(最好是PHP)

As Benjamin Gruenbaum points out, the use of exceptions for flow control should be regarded as a last resort, however does the Promises/A proposal (in some implementations) not legitimize such exploitation of exceptions in this way? 正如本杰明·格鲁恩鲍姆(Benjamin Gruenbaum)所指出的那样,将异常用于流控制应被视为万不得已,但是Promises / A提案(在某些实施方案中)是否不以这种方式使这种利用异常合法化?

I do take on board that an (otherwise uncaught) error thrown from a child function is maybe not what the Promises/A authors had in mind (ie we're still arguably talking about a "dirty trick"), however the Promises/A proposal doesn't specifically exclude such error propagation either. 我确实认为,子函数引发的(否则未捕获的)错误可能不是Promises / A作者想到的(即,我们仍然可以说是“肮脏的把戏”),但是Promises / A提案也没有明确排除此类错误传播。

The Q lib would be ideal for this problem, allowing something like this : Q lib对于这个问题是理想的,允许这样的事情:

Q.fcall(parentFunction).then(function (value) {
    alert(value);
}, function (error) {
    alert(error);
});

fiddle 小提琴

Please note that, for convenience, I also use jQuery in the fiddle but only to give functionality to the buttons. 请注意,为方便起见,我还在小提琴中使用了jQuery,但只是为按钮提供了功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM