Javascript:同时执行和Quine

发布于 2024-09-17 05:03:31 字数 1317 浏览 6 评论 0原文

我这里有一个奇怪的需求,我不确定它的可行性。我只能想如何使用线程来做到这一点(创建另一个执行 Quine 函数的线程,以及运行我想要 Quine 并同时执行的脚本的线程(无需手动到处添加警报!!!),但是javascript 没有这个功能,对吗?

我有一些 JavaScript 是由一个具有最小 JavaScript 调试功能的应用程序解释的,因为它还使用了一些只能由该应用程序理解的语句,所以我无法调试它。在 Firefox 或 Internet Explorer 中,因为它们无法处理这些自定义语句,我希望能够实现一个可以模仿另一个程序但也可以执行它自己的功能的程序

。我有一个 Node.js,它输出“Hello World!”100 次,当提供给应用程序时,它会解释这一点,但在某些时候会失败,但由于调试能力有限,我无法说出原因、地点和时间。

function main(){
     for(var i=0; i<100; i++){
          alert('Hello World!\n');
     }
}

然后,我想要一个可以传递给应用程序的脚本,该脚本将假装是上面的脚本,但也会在执行每个语句之前发出警报。我可以这样做:

function main(){
     alert('main()')
     for(var i=0; i<100; i++){
          alert("alert('Hello World!\n');");
          alert('Hello World!\n');
     }
}

但是我相信您可以看到对于一个很长的程序来说这将是一项艰巨的任务。相反,我想制作一个可以执行如下操作的程序:

function main(){
     var text = loadJSScript("C:\\Script\\Hello.js"); //Loads a text/javascript file line by line into an array
     for(var i=0; i<text.length; i++){
          var statement = text[i];
          alert("Line Number: " + i + " Statement: " + statement); //Output the line number and statement before executing
          execute(statement); //A function that executes the statement from the other file (as if a separate thread)
     }
}

I have a bizarre need here, and I am unsure of it's feasibility. I can only think how I would do it using threads (To create another thread that performs a Quine function along side a thread running the script I want to Quine and execute at the same time (without manually adding alerts everywhere!!!), but javascript doesn't have that functionality, right?

I have some JavaScript which is interpreted by an application with minimal JavaScript debugging capability of it's own. My script is falling over and since it also uses some statements only understood by this application I cannot debug this within Firefox or Internet Explorer because they cannot handle these custom statements. What I am hoping I'll be able to achieve is a program that can mimic another program but also perform it's own functions.

For arguments sake say I have a script called hello.js that outputs "Hello World!" 100 times and when provided to the application it interprets this, however falls over at some point but I can't tell why, where and when because of the limited debug capability.

function main(){
     for(var i=0; i<100; i++){
          alert('Hello World!\n');
     }
}

I then want to have a script that I can pass to the application instead that will pretend to be the script above but will also alert before each statement is executed. I could just do this:

function main(){
     alert('main()')
     for(var i=0; i<100; i++){
          alert("alert('Hello World!\n');");
          alert('Hello World!\n');
     }
}

However I am sure you can see how for a long program this would be an arduous task. Do instead I want to make a program that can perform like this:

function main(){
     var text = loadJSScript("C:\\Script\\Hello.js"); //Loads a text/javascript file line by line into an array
     for(var i=0; i<text.length; i++){
          var statement = text[i];
          alert("Line Number: " + i + " Statement: " + statement); //Output the line number and statement before executing
          execute(statement); //A function that executes the statement from the other file (as if a separate thread)
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-09-24 05:03:31

这实际上取决于您的目标受众(浏览器使用)。

通过使用网络工作者,您可以获得一种非常有效的伪线程形式。

http://ejohn.org/blog/web-workers/

然而,这并不广泛支持。您可以在可用的情况下使用它。

您还可以通过使用 setTimeout 执行代码来获得一些类似于线程的行为。

您可以在此处查看计时器如何排队:
http://ejohn.org/blog/how-javascript-timers-work/< /a>

您还可以在服务器端进行Quine计算,并使用AJAX进行调用。这确实给出了一个异步请求。

最后两个选项不是线程化的,但代码在继续之前不会等待响应。

This really depends on your target audience (browser use).

You can get a very effective form of pseudo-threading by using web workers.

http://ejohn.org/blog/web-workers/

However this is not widely supported. You could use this where available.

You can also get some behaviour similar to threading by using setTimeout to execute the code.

You can see how timers are queued here:
http://ejohn.org/blog/how-javascript-timers-work/

You could also do the Quine calculation server side, and call using AJAX. This does give an asynchronous request.

These last 2 options aren't threading, but the code will not wait for a response before continuing.

御守 2024-09-24 05:03:31

据我所知,JavaScript 是纯粹的单线程。
此外, loadJSScript("C:\\Script\\Hello.js"); 语句很可疑,因为您提到了浏览器。浏览器中的 JS(至少在网页中)无法访问文件系统(此外,这是不可移植的!)。
不过,您也许可以在 Microsoft 的 JScript(在 WSH 中)中执行类似的操作。

To my knowledge, JavaScript is purely mono-threaded.
Moreover, the loadJSScript("C:\\Script\\Hello.js"); statement is fishy, since you mention browsers. JS in browsers (in Web pages, at least) cannot access the filesystem (beside, that's not portable!).
You can perhaps do something like that in Microsoft's JScript (in WSH), though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文