您的位置:首页 > Web前端 > Node.js

Node.js学习笔记——交互式运行环境REPL

2020-01-14 19:38 666 查看

Node.js学习笔记——交互式运行环境REPL

在REPL中:

>foo='bar';
'bar'
>var foo='bar';
undefined

使用下划线字符(访问最近使用的表达式)

>a=3;
3;
>_+=1;
4
>a
3

直接运行函数

`当表达式未书写完成时,环境会自动为表达式的每一行添加英文省略符。`
>function saybbye(){
... console.log('asd');
... console.log('asd');
... }
undefined
>//continue。。。。

REPL环境中的上下文对象

在Node.js的模块文件中,start方法返回被开启的REPL运行环境,可以为REPL运行环境指定一个上下文对象(context),可以将该上下文对象保存的变量作为REPL运行环境中的全局变量来进行使用。

//下面是index.js
var repl=require('repl');
var con=repl.start();
test=con.context;
hello=10;
//这里是为了说明一定是start方法返回的REPL运行环境的上下文对象才可以在REPL中被使用
test.msg='kingsley';
test.testFunction=function(){console.log(test.msg);
}
//REPL环境
C:\Users\kingsley\Desktop\新建文件夹> node index.js
> hello
Thrown:
ReferenceError: hello is not defined
> msg
'kingsley'
> testFunction()
kingsley
undefined

基础命令

  • .break
    退出多行编辑环境(可以用ctrl+c代替)

  • .clear
    清楚REPL运行环境的上下文对象中保存的所有变量与函数(也可以推出多行编辑环境)

  • .exit
    退出REPL运行环境

  • .help
    显示所有基础命令

  • .save
    保存当前输入的所有表达式

>function print(){
... console.log('hello kingsley');
... }
undefined
> .save ../hello.js
Session saved to: ../hello.js
  • load
    把某个文件中保存的所有表达式依次加载到REPL运行环境中
c:\Users\kingsley\Desktop\新建文件夹> node
> .load../hello.js   //load后面要有一个空格
Invalid REPL keyword
> .load ../hello.js
function print(){
console.log('hello kingsley');
}

undefined
> print()
hello kingsley
undefined
>



  • 点赞
  • 收藏
  • 分享
  • 文章举报
kingsleyljc 发布了12 篇原创文章 · 获赞 1 · 访问量 173 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: