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/count/index.html | 169 +++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 files/zh-cn/web/api/console/count/index.html (limited to 'files/zh-cn/web/api/console/count/index.html') diff --git a/files/zh-cn/web/api/console/count/index.html b/files/zh-cn/web/api/console/count/index.html new file mode 100644 index 0000000000..9b28cc12df --- /dev/null +++ b/files/zh-cn/web/api/console/count/index.html @@ -0,0 +1,169 @@ +--- +title: Console.count() +slug: Web/API/Console/count +translation_of: Web/API/Console/count +--- +
{{APIRef("Console API")}}{{Non-standard_header}}
+ +

输出 count() 被调用的次数。此函数接受一个可选参数 label。

+ +

{{AvailableInWorkers}}

+ +

如果有 label,此函数输出为那个指定的 label 和 count() 被调用的次数。

+ +

如果 label 被忽略,此函数输出 count() 在其所处位置上被调用的次数。

+ +

例如,下面的代码:

+ +
var user = "";
+
+function greet() {
+  console.count();
+  return "hi " + user;
+}
+
+user = "bob";
+greet();
+user = "alice";
+greet();
+greet();
+console.count();
+ +

Console 的输出如下:

+ +
"<no label>: 1"
+"<no label>: 2"
+"<no label>: 3"
+"<no label>: 1"
+
+ +

注意最后一行的日志输出:在 11 行对 count() 的单独调用是被当作一个独立事件来处理的。

+ +

如果把变量 user 当作 label 参数传给前面调用的 count(),把字符串 "alice" 传给后面调用的 count():

+ +
var user = "";
+
+function greet() {
+  console.count(user);
+  return "hi " + user;
+}
+
+user = "bob";
+greet();
+user = "alice";
+greet();
+greet();
+console.count("alice");
+ +

可以看到输出如下:

+ +
"bob: 1"
+"alice: 1"
+"alice: 2"
+"alice: 3"
+ +

现在可以基于不同的 label 值维护不同的数值。由于 11 行的 label 匹配了两次 user 的值,因此它不会被当作独立的事件。

+ +

语法

+ +
console.count([label]);
+
+ +

参数

+ +
+
label
+
+ +

    字符串,如果有,count() 输出此给定的 label 及其被调用的次数。

+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Console API", "#consolecountlabel", "console.count()")}}{{Spec2("Console API")}}Initial definition
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("30.0")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
Available in workers{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("38.0")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("30.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
Available in workers{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("38.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
-- cgit v1.2.3-54-g00ecf