从子函数内部从父函数返回 - Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20999298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning from a parent function from inside a child function - Javascript
提问by user3025700
I'm relatively new to coding in JavaScript, and I've came across a problem. I like to nest functions to keep things orderly, but how would I exit from a parent function from inside a child function?
我对 JavaScript 编码比较陌生,但遇到了一个问题。我喜欢嵌套函数以保持有序,但是如何从子函数内部退出父函数?
example:
例子:
function foo1() {
function foo2() {
//return foo1() and foo2()?
}
foo2();
}
回答by T.J. Crowder
See update under the fold
查看折叠下的更新
You can't. You can only return from the child function, and then return from the parent function.
你不能。您只能从子函数返回,然后从父函数返回。
I should note that in your example, nothing ever calls (As of your edit, something does). Let's look at a more real example (and one that comes up a lot): Let's say we want know if an array contains an entry matching some criterion. A first stab might be:foo2
我应该注意到,在您的示例中,没有任何东西会foo2
调用(As of your edit, something does)。让我们看一个更真实的例子(一个经常出现的例子):假设我们想知道一个数组是否包含匹配某个条件的条目。第一次尝试可能是:
function doesArrayContainEntry(someArray) {
someArray.forEach(function(entry) {
if (entryMatchesCondition(entry)) {
return true; // Yes it does <-- This is wrong
}
});
return false; // No it doesn't
}
You can't directly do that. Instead, you have to return from your anonymous iterator function in a way to stop the forEach
loop. Since forEach
doesn't offer a way to do that, you use some
, which does:
你不能直接这样做。相反,您必须以某种方式从匿名迭代器函数返回以停止forEach
循环。由于forEach
没有提供这样做的方法,因此您使用some
,它可以:
function doesArrayContainEntry(someArray) {
return someArray.some(function(entry) {
if (entryMatchesCondition(entry)) {
return true; // Yes it does
}
});
}
some
returns true
(and stops looping) if any call to the iterator function returns true
; it returns false
if no call to the iterator returned true
.
some
true
如果对迭代器函数的任何调用返回,则返回(并停止循环)true
;false
如果没有调用返回的迭代器,则返回true
。
Again, that's just one common example.
同样,这只是一个常见的例子。
You've referred to setInterval
below, which tells me that you're almost certainly doing this in a browser environment.
您在setInterval
下面提到过,这告诉我您几乎可以肯定是在浏览器环境中执行此操作。
If so, your play
function almost certainly has already returned by the time you want to do what you're talking about, assuming the game has any interaction with the user other than alert
and confirm
. This is because of the asynchronous nature of the environment.
如果是这样,那么您的play
函数几乎可以肯定在您想要执行您所说的操作时已经返回,假设游戏与除alert
和之外的用户有任何交互confirm
。这是因为环境的异步性质。
For example:
例如:
function play() {
var health = 100;
function handleEvent() {
// Handle the event, impacting health
if (health < 0 {
// Here's where you probably wanted to call die()
}
}
hookUpSomeEvent(handleEvent);
}
The thing is, that play
will run and return almost immediately. Then the browser waits for the event you hooked up to occur, and if it does, it triggers the code in handleEvent
. But play
has long-since returned.
问题是,它play
几乎会立即运行并返回。然后浏览器等待您连接的事件发生,如果发生,它会触发handleEvent
. 但是,play
长期以来,由于返回。
回答by Brian Tracy
Make a note whether the parent function should also return.
记下父函数是否也应该返回。
function foo1() {
bool shouldReturn = false;
function foo2() {
shouldReturn = true; // put some logic here to tell if foo1() should also return
return;
}
if (shouldReturn) {
return;
} else {
// continue
}
}
回答by Mike Miller
Based on your comment, something like this might work as a main game loop.
根据您的评论,这样的事情可能会作为主要的游戏循环。
function play() {
var stillPlaying = true;
while(stillPlaying) {
... play game ...
stillPlaying = false; // set this when some condition has determined you are done
}
}