- 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.warn() 方法
Node.js 的 console.warn() 方法會將警告訊息作為輸出列印到 控制檯。它將輸出列印到 stderr,並新增換行符。此方法提供的 message 引數必須是字串,或者是可以使用 util.inspect() 函式轉換為字串的物件,才能正常工作。它與 node.js 的 console.error() 方法非常相似。此方法在日常網頁中很有用,可以顯示控制檯上的錯誤訊息。
語法
以下是 Node.js console.warn() 方法的語法:
console.warn( [data][, ...args] )
引數
此方法接受多個引數,如下所述。
在 data 引數中,我們傳遞應顯示在控制檯上的訊息。
第二個引數 args 是我們傳遞到 data 引數中的訊息的替換值。
返回值
此函式將在控制檯上返回一條警告訊息,其中包含我們傳遞給它的引數。
示例
在下面的示例中,我們將訊息傳遞到方法的 data 引數中。
console.warn("This is an error statement");
輸出
正如我們在輸出中看到的,Node.js console.warn() 方法列印了包含傳遞到控制檯的訊息的錯誤。
This is an error statement
示例
在下面的示例中,我們正在執行一個迴圈,在迴圈內部,我們使用 data 引數呼叫 console.warn() 方法。
for(i = 1; i <= 10; i++)
{
console.warn("This is error statement: " + i);
}
輸出
正如我們在輸出中看到的,對於每次迭代,我們都會收到包含我們傳遞給函式的訊息的警告。
This is error statement: 1 This is error statement: 2 This is error statement: 3 This is error statement: 4 This is error statement: 5 This is error statement: 6 This is error statement: 7 This is error statement: 8 This is error statement: 9 This is error statement: 10
示例
在下面的示例中,
我們聲明瞭兩個整型變數,並對它們執行乘法和減法運算。
然後,我們使用 'if' 語句,如果條件滿足,則執行 console.warn() 方法。
var a = 10;
var b = 15;
var c = a * b;
var d = b - a;
if (c > d){
console.warn( c + " is %s than " + d, 'greater');
}
輸出
正如我們在下面的輸出中看到的,條件滿足,並且執行了 console.warn() 方法。
150 is greater than 5
nodejs_console_module.htm
廣告
