1.新建js文件
使用WebStorm打开一个新目录(例如 F:\test\nodejs),新建一个javascript文件(hello.js),敲代码如下
'use strict';
console.log("hello, world");
'use strict';表示以严格模式运行JavaScript代码。
2.切换到hello.js目录并运行hello.js
cmd> C:\>cd /d F:/test/nodejs
cmd> node hello.js
3.关于严格模式的使用
如果每个JavaScript文件都写'use strict';太麻烦,有个简单的办法,就是在cmd上直接执行一行命令,本机上后续执行JavaScript脚本都会以strict模式执行。注意是没执行一次js文件,在命令行自行增加 -use_strict
node -use_strict hello.js
后续代码,如无特殊说明,我们都会直接给Node传递--use_strict
参数来开启严格模式。