如何在javascript中使用return
function hello1() {
function hello2() {
if (condition) {
return; // How can I exit from hello1 function not hello2 ?
}
}
}发布于 2010-12-08 01:02:32
你不能,这不是return的工作方式。它只从当前函数中退出。
能够从调用栈上更高的函数返回将破坏该函数提供的encapsulation (即,它不需要知道它是从哪里被调用的,它应该由调用者决定如果该函数失败了该怎么做)。函数的部分意义在于调用者不需要知道函数是如何实现的。
您可能需要的内容如下所示:
function hello1() {
function hello2() {
if (condition) {
return false;
}
return true;
}
if (!hello2()) {
return;
}
}https://stackoverflow.com/questions/4379359
复制相似问题