简体   繁体   English

可以在JavaScript中完成同步输入吗?

[英]Can a synchronous input be done in JavaScript?

I'm interested in the possibility of using JavaScript for an introductory language for students. 我对将JavaScript用作学生的入门语言的可能性很感兴趣。 The missing piece for me is the ability to provide a blocking equivalent to the input() command that is available in other languages. 对我来说,缺少的部分是能够提供与其他语言中可用的input()命令等效的阻塞功能。

Using console-only versions of JavaScript, like SpiderMonkey , would be a solution. 使用仅限控制台的JavaScript版本(例如SpiderMonkey )将是一个解决方案。 SpiderMonkey has a readline() command. SpiderMonkey具有readline()命令。 But I think the students would be more interested in code that ran inside a web browser. 但是我认为学生会对Web浏览器中运行的代码更感兴趣。 It would be nice if students could share with their friends their version of the classic "guess a number between 1 and 10" game. 如果学生可以与朋友分享他们经典的“猜数字介于1到10之间”游戏的版本,那就太好了。

The JavaScript window.prompt() command would also work, but the fact that it pops up a completely separate window isn't ideal. JavaScript window.prompt()命令也可以使用,但是它弹出一个完全独立的窗口的事实并不理想。 I'd like an in-document input field that works the same way. 我想要一个以相同方式工作的文档内输入字段。 Is there such a solution? 有这样的解决方案吗?

prompt() is the only thing that comes to mind as a blocking input method. 唯一的prompt()是阻塞输入法。 I don't exactly know why you need to make it blocking, but perhaps you can use the jQuery blocking plugin to achieve the blocking (at least the UI)? 我不完全知道为什么需要使其阻塞,但是也许您可以使用jQuery阻塞插件来实现阻塞(至少是UI)?

I don't think you will find a great way to do that and I'm not sure you should try. 我认为您不会找到一种很好的方法来做到这一点,而且我不确定您应该尝试一下。 I assume this is a very basic course in programming and you want the students to get the feeling they can accomplish something. 我认为这是编程中非常基础的课程,您希望学生获得可以完成某件事的感觉。 In that case I would provide them with html code that contains the input field, button and an onclick handler hooked to a function already. 在那种情况下,我将为他们提供html代码,其中包含输入字段,按钮和已经挂接到函数的onclick处理程序。

Students will quickly learn to add input fields and buttons. 学生将很快学会添加输入字段和按钮。 Just because you and I learned to program in text only environments doesn't mean that todays students should. 仅仅因为您和我学会了在纯文本环境中编程,并不意味着今天的学生应该这样做。

You con use an input text and a button. 您使用输入文本和按钮。 when button is pressed get and process the text and clean the input text for a new usage. 当按下按钮时,获取并处理文本,并清洁输入文本以用于新用途。

In addition you can consider use http://jsfiddle.net/ 另外,您可以考虑使用http://jsfiddle.net/

In a web browser, you can use prompt . 在网络浏览器中,可以使用prompt It's very old-fashioned (like alert ) and behaves (almost entirely) synchronously. 它是非常老式的(例如alert ),并且(几乎完全是)同步运行。 So: 所以:

var str;
str = prompt("Enter some text: ", "");
console.log("You entered '" + str + "'");
str = prompt("Enter some more text: ", "");
console.log("You entered '" + str + "'");

Live example | 现场示例 | Live source 现场直播

Instead, though, I think I'd probably recommend getting the students used to using callbacks, and providing some utility code (or just using some pre-built thing). 相反,尽管如此,我认为我可能建议让学生习惯使用回调,并提供一些实用程序代码(或仅使用一些预构建的东西)。 The sooner they get used to it, the better, and it's not that complicated. 他们越早习惯它,就越好,而且没有那么复杂。 For instance, the asynchronous version of the above could readily be: 例如,上述的异步版本可以很容易地成为:

asynchronousPrompt("Enter some text: ", "", function(text) {
    console.log("You entered '" + text + "'");
    asynchronousPrompt("Enter some more text: ", "", function(text) {
        console.log("You entered '" + text + "'");
    });
});

JavaScript中的同步执行为使用JavaScript执行同步操作提供了不错的编写和示例代码。

Would NodeJs be a solution for you? NodeJ将为您提供解决方案吗? It supports a REPL afaik. 它支持REPL afaik。 And it also has a STDIO implementation 它还具有STDIO实现

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

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