How run a windows console for JavaScript

女生的网名这么多〃 提交于 2019-12-11 04:04:04

问题


I would like to have a console window (a command line) on Windows 7 which will allow me to play with JavaScript just like a python console.

Update: It's important to have a file access from within the console (or script run through it).


回答1:


You can use Node.js's REPL. To do so follow this steps:

  1. Download and Install Node.js.
  2. Call Node.js from the Start Menu / Start Screen or directly node.exe installation path (e.g C:\Program Files\nodejs\node.exe).
  3. Enjoy!

You may want to add the installation path to your PATH enviroment variable for ease of use.

Note: to leave node.js press Ctrl + C twice.


To access the local files, you will need the File System module. This is an example of usage:

var fs = require("fs");
fs.readFile(
    "C:\\test.txt",
    function(err, data)
    {
        if (!err)
        console.log(data.toString());
    }
);

This will output the contents of the file C:\test.txt to the console.

Note: An unhandled exception will cause node.js to "crash".




回答2:


You can just use the developer tools.

For example, in Chrome, press F12. This will bring up the developer tools. The last option on the menubar is console. This will allow you to create JS variables and functions and to interact with DOM elements on the current page




回答3:


It's possible thanks to Mozilla Rhino JavaScript Engine.

To create a console window for JS:

1) Download Mozilla Rhino JavaScript Engine binary.

2) Extract: js.jar.

3) Create a script to run the console window (e.g. rihno_console.bat):

java -cp js.jar org.mozilla.javascript.tools.shell.Main

For more information about usage (for instance, and global functions inside this console) visit the Rhino Shell web page.




回答4:


Just like I informed another user with the same question as yours who was faced with the same need, check out DeskJS (https://deskjs.wordpress.com). It's a portable Windows console application that lets you run pure JavaScript code and even load any existing JS files. It supports even the basic JS popup boxes implemented in browsers. You can save your commands as JS files that can be run on startup or by dragging-and-dropping them on the app. Plus there's so much more to it like you can create a build system for Sublime Text that can run JS files via cmd, it supports themes for customizing the entire console and snippets which let you save short snippets of JavaScript code for later use. Improvements are still being made on the app together with other native APIs being included. Hope this helps you as it did for me.



来源:https://stackoverflow.com/questions/20737607/how-run-a-windows-console-for-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!