- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - assert.deepStrictEqual() 函式
Node.js assert.deepStrictEqual() 函式將對它的兩個引數 actual 和 expected 進行深度嚴格相等性測試。由於此方法在比較方面存在大量差異,因此最好優先使用 assert.deepStrictEqual() 而不是 assert.deepEqual()。
語法
以下是 Node.js assert.deepStrictEqual() 函式 的語法:
assert.deepStrictEqual(actual, expected[, message]);
引數
此函式接受三個引數。下面將對它們進行描述。
Actual − (必需) 此引數中傳遞的值將被評估。該值可以是任何型別。
Expected − (必需) 此引數中傳遞的值將與 actual 值進行比較。該值可以是任何型別。
Message − (可選) 可以將字串或錯誤型別作為輸入傳遞到此引數中。
返回值
如果 actual 和 expected 不匹配,則此函式將在終端返回 AssertionError。
示例
在下面的示例中,我們建立了兩個具有相同屬性的物件,並且我們更改了某個特定屬性的型別(在第一個物件中為整數,在第二個物件中為字串)。然後,我們將這兩個物件作為 actual 和 expected 傳遞給 Node.js assert.deepStrictEqual() 函式,並將文字傳遞給 message 引數。
const assert = require('assert');
var object1 = {
Name: 'Roy',
Age: 34,
Nickname: 'Godsway'
};
var object2 = {
Name: 'Roy',
Age: '34',
Nickname: 'Godsway'
}
assert.deepStrictEqual(object1, object2, 'Both the objects are not identical');
輸出
當我們編譯並執行程式碼時,該函式將丟擲 AssertionError 以及訊息到輸出,因為 actual 和 expected 不相同。這是因為 34 !== '34'。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the objects are not identical at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:15:8) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
示例
在下面的示例中,我們建立了兩個具有相同屬性的物件。
const assert = require('assert');
var object1 = {
Name: 'Roy',
Age: 34,
Nickname: 'Godsway'
};
var object2 = {
Name: 'Roy',
Age: 34,
Nickname: 'Godsway'
}
assert.deepStrictEqual(object1, object2, 'Both the objects are identical');
輸出
當我們編譯並執行程式碼時,該函式不會將 AssertionError 丟擲到輸出,因為 actual 和 expected 是相同的。這是因為 34 == 34。
// Returns nothing
示例
在下面的示例中,我們將一個 字串 和一個物件傳遞給函式進行比較。
const assert = require('assert');
assert.deepStrictEqual(new String('Tutorialspoint'), Object('Tutorialspoint'));
輸出
當我們編譯並執行程式碼時,該函式不會將 AssertionError 丟擲到輸出,因為 actual 和 expected 是相同的。這是因為在展開後,物件和字串是相同的。
// Returns nothing
示例
在下面的示例中,我們將 0 和 -0 傳遞給函式進行比較。
const assert = require('assert');
var zero = 0;
var negzero = -0
assert.deepStrictEqual(zero, negzero, 'Both are not same');
輸出
當我們編譯並執行程式碼時,該函式將丟擲 AssertionError 以及 message 到輸出,因為 actual 和 expected 不相同。這是因為 0 !== -0。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both are not same at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:5:8) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
示例
在下面的示例中,我們將 -0 和 -0 傳遞給函式進行比較。
const assert = require('assert');
var neggzero = -0;
var negzero = -0
assert.deepStrictEqual(neggzero, negzero, 'Both are same');
輸出
當我們編譯並執行程式碼時,該函式不會將 AssertionError 丟擲到輸出,因為 actual 和 expected 是相同的。這是因為 -0 == -0。
// Returns nothing
示例
在下面的示例中,我們將 5 和 6 傳遞給函式進行比較。但是我們沒有將任何文字傳遞到 message 引數中。因此,該函式將分配一個預設錯誤訊息。
const assert = require('assert');
assert.deepStrictEqual(5, 6);
輸出
當我們編譯並執行程式碼時,該函式將丟擲 AssertionError 以及該函式分配給輸出的 預設錯誤訊息,因為 actual 和 expected 不相同。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B: + expected - actual - 5 + 6 at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:3:8) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
