简体   繁体   English

脱离一个JS函数以调用另一个JS函数

[英]Breaking out of one JS function to call another JS function

Let's say that I have a series of functions: 假设我有一系列功能:

function a(){
   //do this, do that
   b();
}
function b(){
   //do this, do that
   c();
}
function c(){
   //do this, do that
   // program ends at this point
}
a();

You would assume that from executing the code, we go from a() to b() to c() and the program terminates... 您将假设执行代码后,我们从a()b()再到c() ,程序终止了……

What I'm wondering is... is it possible to place code somewhere within the // do this, do that part of b() that depending on certain conditions to return to a() and then proceed through without the need of returning where it left off in b() , although if needed could then launch b() afresh? 我想知道的是...是否可以将代码放置在// do this, do that内的某个位置, // do this, do that b()那部分工作b()取决于某些条件,然后返回到a() ,然后继续进行而无需返回它在b()保留的位置,但是如果需要的话可以重新启动b()

I know, what you're thinking... "This is a JavaScript GOTO question, surely!" 我知道,您在想什么……“肯定是JavaScript GOTO问题!” and to a degree it is... but seriously, is it possible to break from a function A completely and go to function B without having to worry about it returning to function A to finish where it left off? 并在某种程度上是……但很严重,是否有可能完全脱离功能A而转到功能B,而不必担心它会返回功能A以结束它的停止?

You can use JavaScript's return statement like this 您可以像这样使用JavaScript的return语句

function a(){
   //do this, do that
   b();
}
function b(){
   if (some_random_condition) {
      return;
   }
   c();
}
a();

The return statement makes it possible to go to a without having to execute the rest of b function. return语句可以转到a而不必执行b功能的其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM