From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/console/assert/index.html | 102 ++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 files/zh-cn/web/api/console/assert/index.html (limited to 'files/zh-cn/web/api/console/assert') diff --git a/files/zh-cn/web/api/console/assert/index.html b/files/zh-cn/web/api/console/assert/index.html new file mode 100644 index 0000000000..4ce50bbef2 --- /dev/null +++ b/files/zh-cn/web/api/console/assert/index.html @@ -0,0 +1,102 @@ +--- +title: Console.assert() +slug: Web/API/Console/assert +tags: + - console +translation_of: Web/API/console/assert +--- +
{{APIRef("Console API")}}
+ +

如果断言为false,则将一个错误消息写入控制台。如果断言是 true,没有任何反应。

+ +

{{AvailableInWorkers}}

+ +
+

console.assert()方法在Node.js中的实现和浏览器中可用的console.assert()方法实现是不同的。在浏览器中当console.assert()方法接受到一个值为假断言的时候,会向控制台输出传入的内容,但是并不会中断代码的执行,而在Node.js v10.0.0之前,一个值为假的断言也将会导致一个AssertionError被抛出,使得代码执行被打断。v10.0.0修复了此差异,所以现在console.assert()在Node 和浏览器中执行行为相同 。

+
+ +

语法

+ +
console.assert(assertion, obj1 [, obj2, ..., objN]);
+console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting
+ +

参数

+ +
+
assertion
+
一个布尔表达式。如果assertion为假,消息将会被输出到控制台之中。
+
obj1 ... objN
+
被用来输出的Javascript对象列表,最后输出的字符串是各个对象依次拼接的结果。
+
msg
+
一个包含零个或多个子串的Javascript字符串。
+
subst1 ... substN
+
各个消息作为字串的Javascript对象。这个参数可以让你能够控制输出的格式。
+
+ +

案例

+ +

下面的代码示例演示了JavaScript对象的使用:

+ +
const errorMsg = 'the # is not even';
+for (let number = 2; number <= 5; number += 1) {
+    console.log('the # is ' + number);
+    console.assert(number % 2 === 0, {number: number, errorMsg: errorMsg});
+    // 或者使用 ES2015 对象简写:
+    // console.assert(number % 2 === 0, {number, errorMsg});
+}
+// 输出:
+// the # is 2
+// the # is 3
+// Assertion failed: {number: 3, errorMsg: "the # is not even"}
+// the # is 4
+// the # is 5
+// Assertion failed: {number: 5, errorMsg: "the # is not even"}
+ +

请注意, 你可以在大多数浏览器中使用 console.log 进行格式化输出

+ +
console.log('the word is %s try number %d', 'foo', 123);
+// 输出: the word is foo try number 123
+ +

但是 console.assert 在不同浏览器中可能获得不同的效果:

+ +
console.assert(false, 'the word is %s', 'foo');
+// correct output in Node (e.g. v8.10.0) and some browsers
+//     (e.g. Firefox v60.0.2):
+// Assertion failed: the word is foo
+// incorrect output in some browsers
+//     (e.g. Chrome v67.0.3396.87):
+// Assertion failed: the word is %s foo
+ +

有关详细信息,请参阅 {{Domxref("console")}} 文档中的 输出文本到控制台

+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Console API", "#consoleassertexpression-object", "console.assert()")}}{{Spec2("Console API")}}Initial definition
+ +

浏览器兼容性

+ +

{{Compat("api.Console.assert")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf