如何在Javascript中使用goto? [英] How can I use goto in Javascript?

查看:1121
本文介绍了如何在Javascript中使用goto?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我绝对必须使用 goto 来实现。例如,我想写一个这样的程序:

I have some code that I absolutely must implement using goto. For example, I want to write a program like this:

start:
alert("RINSE");
alert("LATHER");
repeat: goto start

有没有办法在Javascript中执行此操作?

Is there a way to do that in Javascript?

推荐答案

绝对!有一个名为 Summer of Goto 的项目允许您使用JavaScript它的最大潜力将彻底改变您编写代码的方式。

Absolutely! There is a project called Summer of Goto that allows you use JavaScript at its fullest potential and will revolutionize the way you can write your code.

这个JavaScript预处理工具允许您创建标签,然后使用以下语法转到它:

This JavaScript preprocessing tool allows you to create a label and then goto it using this syntax:

[lbl] <label-name>
goto <label-name>

例如,问题中的示例可以写成如下:

For example, the example in the question can be written as follows:

[lbl] start:
alert("LATHER");
alert("RINSE");
[lbl] repeat: goto start;

请注意,您不仅限于简单的琐碎程序,如无尽的 LATHER RINSE 重复循环 - goto 提供的可能性是无穷无尽的,你甚至可以制作一个 Hello,world!向JavaScript控制台发送538次消息,如下所示:

Note that you are not just limited to simple trivial programs like an endless LATHER RINSE repeat cycle—the possibilities afforded by goto are endless and you can even make a Hello, world! message to the JavaScript console 538 times, like this:

var i = 0;
[lbl] start:
console.log("Hello, world!");
i++;
if(i < 538) goto start;

你可以阅读更多关于如何实现goto的信息,但基本上,它会进行一些JavaScript预处理,利用你可以用标记为,而循环。所以,当你写下Hello,world!上面的程序,它被翻译成这样的东西:

You can read more about how goto is implemented, but basically, it does some JavaScript preprocessing that takes advantage of the fact that you can simulate a goto with a labelled while loop. So, when you write the "Hello, world!" program above, it gets translated to something like this:

var i = 0;
start: while(true) {
  console.log("Hello, world!");
  i++;
  if(i < 538) continue start;
  break;
}

此预处理过程存在一些限制,因为while循环无法跨越多个功能或块。但这并不是什么大不了的事 - 我确信在JavaScript中利用 goto 的好处绝对会让你无所适从。

There are some limitations to this preprocessing process, because while loops cannot stretch across multiple functions or blocks. That's not a big deal, though—I'm sure the benefits of being able to take advantage of goto in JavaScript will absolutely overwhelm you.

以上所有导致goto.js库的链接都是DEAD,这里是需要的链接:

All above link that lead to goto.js library is ALL DEAD, here is links needed:

goto.js(未压缩) --- parseScripts.js(未压缩)

来自 Goto.js


PS对于任何想知道的人(到目前为止总共为零人),Goto的夏天是一个由Paul Irish推广的术语,同时讨论这个脚本和PHP决定将goto添加到他们的语言中。

P.S. For anyone who is wondering (so far a total of zero people), Summer of Goto is a term that was popularized by Paul Irish, while discussing this script and PHP’s decision to add goto into their language.

对于那些没有立即认识到这一切都是个笑话的人,请原谅我。 < ;-(保险)。

And for those who do not immediately recognize that this entire thing is a joke, please forgive me. <—(insurance).

这篇关于如何在Javascript中使用goto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆