node.js自学笔记(1)-Helloworld

来源:互联网 发布:人工智能会计师 编辑:程序博客网 时间:2024/05/29 09:25

因为年后开始研究Appium框架,因为Appium客户端是用node.js编写的,所以要学习一下node.js的知识。

安装

下载完安装包安装就行,安装完成以后在cmd下敲如下命令:

D:\node.js\0210>node -vv0.12.0

如果显示版本号,那就说明安装成功了。

Helloworld

直接命令行

D:\node.js\0210>node> console.log('HelloWorld')HelloWorldundefined>

文件

推荐采用这种方式

新建一个js文件,取名为helloworld.js

helloworld.js

然后在命令行模式下定位到文件所在目录,然后执行命令

D:\node.js\0210>node helloworld.jsHelloworldD:\node.js\0210>

断言

新建一个assert.js ,输入以下内容:

var assert = require('assert')

fail

assert.fail(actual, expected, message, operator)
永远都会抛出异常

var assert = require('assert')assert.fail(5,5,null,'===')assert.fail(5,5,undefined,'===')assert.fail(5,5,"test",'===')

执行node命令后输出:

D:\node.js\0209>node assert.jsassert.js:86  throw new assert.AssertionError({        ^AssertionError: 5 === 5    at Object.<anonymous> (D:\node.js\0209\assert.js:2:8)    at Module._compile (module.js:460:26)    at Object.Module._extensions..js (module.js:478:10)    at Module.load (module.js:355:32)    at Function.Module._load (module.js:310:12)    at Function.Module.runMain (module.js:501:10)    at startup (node.js:129:16)    at node.js:814:3

然后我们注释第二行:

var assert = require('assert')//assert.fail(5,5,null,'===')assert.fail(5,5,undefined,'===')assert.fail(5,5,"test",'===')

这个时候我们执行结果和上面是一样的。说明nullundefined 是一样的效果。这个时候我们注释第三行:

var assert = require('assert')//assert.fail(5,5,null,'===')//assert.fail(5,5,undefined,'===')assert.fail(5,5,"test",'===')

这个时候的输出就不一样了:

D:\node.js\0209>node assert.jsassert.js:86  throw new assert.AssertionError({        ^AssertionError: test    at Object.<anonymous> (D:\node.js\0209\assert.js:4:8)    at Module._compile (module.js:460:26)    at Object.Module._extensions..js (module.js:478:10)    at Module.load (module.js:355:32)    at Function.Module._load (module.js:310:12)    at Function.Module.runMain (module.js:501:10)    at startup (node.js:129:16)    at node.js:814:3D:\node.js\0209>

所以从上面的例子可以看出,fail断言不管参数是什么,都会跳出程序的执行,只不过打印的异常信息不一样而已

构造方法

assert(value, message)
判断第一个参数value是否为真,如果不为真就跳出程序,并输出message

上面之所以叫构造方法,因为我认为它用了assert自身,没有加入. 号来调用方法。所以暂且用构造方法来定义该小节吧。

var assert = require('assert')assert(2>3,'2 not bigger than 3')

执行该程序:

D:\node.js\0209>node assert.jsassert.js:86  throw new assert.AssertionError({        ^AssertionError: 2 not bigger than 3    at Object.<anonymous> (D:\node.js\0209\assert.js:5:1)    at Module._compile (module.js:460:26)    at Object.Module._extensions..js (module.js:478:10)    at Module.load (module.js:355:32)    at Function.Module._load (module.js:310:12)    at Function.Module.runMain (module.js:501:10)    at startup (node.js:129:16)    at node.js:814:3D:\node.js\0209>

如果我们把2和3的位置颠倒,它就不会抛出异常了。

var assert = require('assert')assert(3>2,'2 not bigger than 3')
D:\node.js\0209>node assert.jsD:\node.js\0209>

ok

assert.ok(value, [message])
和上面构造方法的方式是一样的

equal

assert.equal(actual, expected, [message])
判断actual和expected的值是否相等,如果不相等就抛出异常并打印message
浅测试,等同于使用’==’进行相等判断

下面的程序乍一个看有点懵,主要是检测hello和hello1是否相等,如果不相等打印hello2,并抛出异常,那么hello和hello1自然是不相等的。那执行下面程序会抛出异常

var assert = require('assert')assert.equal('hello','hello1','hello2')
D:\node.js\0209>node assert.jsassert.js:86  throw new assert.AssertionError({        ^AssertionError: hello2    at Object.<anonymous> (D:\node.js\0209\assert.js:2:8)    at Module._compile (module.js:460:26)    at Object.Module._extensions..js (module.js:478:10)    at Module.load (module.js:355:32)    at Function.Module._load (module.js:310:12)    at Function.Module.runMain (module.js:501:10)    at startup (node.js:129:16)    at node.js:814:3D:\node.js\0209>

notEqual

assert.notEqual(actual, expected, [message])
浅测试,等同于使用’!=’进行不相等判断
与equal逻辑相反

利用上面的例子,只不过把equal改为notEqual:

var assert = require('assert')assert.notEqual('hello','hello1','hello2')

上面的代码执行后不会报错,因为hello!=hello1

D:\node.js\0209>node assert.jsD:\node.js\0209>

想报错么?想的话,把hello改成hello1就行。

deepEqual/notDeepEqual

深度/非深度匹配

何意呢?何为深度?

就是如果是一个对象,里面的属性都要匹配。

那么notDeepEqual是不是就不是深度匹配?

当然不是,依然采用的是深度匹配原则,跳出程序的逻辑不一样,notDeepEqual会在2个对象属性都相等的情况下跳出程度。至于这两个方法我就不举例,因为我还不知道对象咋定义。

strictEqual/notStrictEqual

使用严格匹配原则

啥是严格匹配啊?

就是利用=== 符号表示相等,!== 来表示不相等。

0 0
原创粉丝点击