我有一个函数parent,它调用child,然后在里面做其他事情otherStuff:
function parent() {
child();
otherStuff();
}
是否可以修改child(并保持parent原样)以便child调用parent在返回后立即child返回?这在 EcmaScript 6 中是否可行?
我有一个函数parent,它调用child,然后在里面做其他事情otherStuff:
function parent() {
child();
otherStuff();
}
是否可以修改child(并保持parent原样)以便child调用parent在返回后立即child返回?这在 EcmaScript 6 中是否可行?
这样做的不好child的方法是在 中抛出一个异常,它会冒泡到parent原始调用者。这种方法期望原始调用者parent捕获异常。
function originalCaller() {
try {
parent();
}
catch(e) {}
}
function parent() {
child();
otherStuff();
}
function child() {
throw 0;
}
function otherStuff() {
// other stuff
}
你想就这样离开parent,对吧?所以,丑陋的方法是让child临时修改otherStuff:
function child() {
_otherStuff = otherStuff;
otherStuff = function() { otherStuff = _otherStuff; }
}
这种方式一次otherStuff什么都不做,然后又回到原来的状态。同样,它不仅完全丑陋,而且对' 的结构做出了假设。parent