.break
和.clear
命令的行为有所不同,具体取决于您是从node
命令启动REPL还是使用repl.start()
。
使用node
命令时,.clear
只是.break
的别名。但是,如果您从repl.start()
开始REPL,则.clear
将清除您所期望的本地上下文,并且.break
的行为如上所述。
这是.help
看起来像一个REPL例如,从node
开始:
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.exit Exit the repl
.help Show repl options
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
这是什么样子的程序启动时:
.break Sometimes you get stuck, this gets you out
.clear Break, and also clear the local context
.exit Exit the repl
.help Show repl options
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
在REPL使用.clear
开始repl.start()
也会显示:
Clearing context...
这里的编程方式使用REPL的例子:
var repl = require('repl');
var str = 'We can pass variables into a context.';
repl.start().context.str2 = str;
在此之后,我们有一个CLI开放。如果我们输入str2
,那么您将得到str
的内容。如果清除上下文,则str2
将不再存在于上下文中。
真是个好回答!这么彻底! :) – shmuli
你介意给我提供一个如何以编程方式启动REPL的例子,这样我就可以测试这个了吗?谢谢! – shmuli
只是想通了。但是,谢谢!我感谢你的帖子。 :) – shmuli