我正在阅读HTML5游戏中的一些源代码,并且在其动画功能中有一条return;
行。我不确定它的作用。
function animate() {
if (somecondition) {
doSomething;
return;
}
renderSomethingElse();
requestAnimationFrame(animate);
}
animate();
return;
行实际上没有返回任何内容并且它不会停止动画功能,是否只是停止运行除requestAnimationFrame(animate);
以外的其余功能?
编辑和结论:
我发现return;
行实际上退出了.forEach
函数中animate()
循环中的匿名函数。这就是为什么它不退出animate()
功能。
答案 0 :(得分:2)
是的,这是提前退出。
$ cat - > new_file
hello
world
<Ctrl-D>
$ cat new_file
hello
world
它可以替代以下条件:
function test(){
alert('works');
return;
alert("doesn't work");
}
test();