Javascript从子函数退出
我有一组bind,它从代码前后的函数中获取触发器。例如:Javascript从子函数退出,javascript,function,parent,Javascript,Function,Parent,我有一组bind,它从代码前后的函数中获取触发器。例如: function global() { before(); // call all before binds here //... mainFunction code... after(); // call all after binds here } 如果before()中的函数之一回调要退出或停止global()的进一步运行,如何在不检查返回值的情况下停止它?在不检查值ed的情况下实现这一点的唯一方法是
function global() {
before(); // call all before binds here
//... mainFunction code...
after(); // call all after binds here
}
如果
before()中的函数之一代码>回调要退出或停止global()
的进一步运行,如何在不检查返回值的情况下停止它?在不检查值ed的情况下实现这一点的唯一方法是通过调用
如果您在某个地方调用了global
,并且希望调用后的任何代码都能继续执行,那么您需要用一个
function global() {
try {
before();
// never reaches here
after();
} catch (e) {
console.log(e); // log error. Leave this block empty for no action
}
}
global(); // Error logged
console.log('bar'); // still executed
在不检查值ed的情况下实现这一点的唯一方法是通过调用
如果您在某个地方调用了global
,并且希望调用后的任何代码都能继续执行,那么您需要用一个
function global() {
try {
before();
// never reaches here
after();
} catch (e) {
console.log(e); // log error. Leave this block empty for no action
}
}
global(); // Error logged
console.log('bar'); // still executed
这不也会停止其他功能吗?来自ouside global()的其他函数;串联的其他功能,是的。我将编辑以获得解决方案that@Basit此外,在复制您的示例时,我使用了global
,但如果您在全局命名空间中实际放置了名为global
的内容,则可能会遇到冲突问题,因此请尝试选择不同的名称/编写模块化代码。这不会也会停止其他函数吗?来自ouside global()的其他函数;串联的其他功能,是的。我将编辑以获得解决方案that@Basit此外,我在复制您的示例时使用了global
,但是如果您在全局名称空间中实际放置了名为global
的内容,则可能会遇到冲突问题,因此请尝试选择不同的名称/编写模块化代码。