- 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 條件查詢
- Node.js - MySQL 排序
- 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 - 討論
NodeJS - console.count() 方法
Node.js 的 console.count() 方法是 node.js 的 Console 類的一個內建方法。
此方法將返回使用特定輸入值呼叫該函式的次數的 計數。
考慮一種情況,我們使用特定值呼叫此函式兩次。它將在輸出中返回計數為 2。
示例
console.count(abc); console.count(abc);
輸出
abc:2
讓我們來看另一個場景,首先我們使用指定的值呼叫此函式,然後使用另一個值呼叫此函式,之後再次使用第一個指定的值呼叫該函式。它儲存每個輸入值的計數。
示例
console.count(abc); console.count(xyz); console.count(abc);
輸出
abc:2 xyz:1
此方法將維護傳遞為引數的特定 標籤 的內部計數,並輸出到 stdout,即使用輸入 標籤 呼叫 console.count() 方法的次數。
為了更好地理解 Node.js console.count() 方法,讓我們瞭解其語法和用法。
語法
以下是 Node.js console.count() 方法的語法:
console.count(label)
引數
label − 這是一個可選引數;它指定要計數的 標籤 的值。預設情況下,標籤的值為“default”。
返回值
此方法將指定 標籤 的計數返回到 stdout。
示例
Node.js 的 console.count() 方法將接受一個可選引數 (label)。
在下面的示例中,我們將一個 字串 值傳遞給 count() 方法的 label 引數。
const console = require('console');
console.count("Tutorialspoint");
console.count("Tutorialspoint");
console.count("Tutorialspoint");
輸出
正如我們在下面的輸出中看到的,我們在每個 count() 方法中都傳遞了相同的值作為引數。因此,該方法返回了輸入值的計數。
Tutorialspoint: 1 Tutorialspoint: 2 Tutorialspoint: 3
示例
即使我們不傳遞任何引數,console.count() 方法也能工作。它將返回輸出為“default”。
在下面的示例中,我們呼叫 console.count() 方法而不帶任何引數進行計數。
const console = require('console');
console.count();
console.count();
console.count();
輸出
正如我們在下面的輸出中看到的,該方法返回了計數,即函式被呼叫的次數。由於我們沒有傳遞任何引數,它將顯示 label 值為“default”。
default: 1 default: 2 default: 3
示例
如果我們傳遞“default”作為標籤引數的值,則 console.count() 方法將將其視為 default。
在下面的示例中,我們呼叫了不帶引數 (label) 的 count() 方法,也透過在標籤中傳遞“default”作為值來呼叫該方法。
const console = require('console');
console.count();
console.count("default");
console.count("default");
console.count();
輸出
正如我們在上面的輸出中看到的;當我們將值“default”傳遞給引數 (label) 時,該方法將將其視為 default。
default: 1 default: 2 default: 3 default: 4
示例
如果我們傳遞一個 整數 作為 引數(label),並且也傳遞了相同的 整數 值作為 字串。console.count() 方法將它們視為相同並返回計數。
在下面的示例中,我們在引數 (label) 中傳遞了一個 整數值和相同的整數值作為字串。
const console = require('console');
console.count(1);
console.count("1");
console.count(1);
輸出
正如我們在下面的輸出中看到的,console.count() 方法將整數值和字串值視為相同並返回計數。
1: 1 1: 2 1: 3
