Node.js – util.deprecate() 方法
util.deprecate() 方法以一種方式包裝 fn(它可以是函式或類),以將其標記為已棄用的方法。util.deprecate() 方法返回一個函式,該函式發出 DeprecationWarning。在首次呼叫該函式時,將此警告列印到 stderr。一旦發出警告,就會在不發出任何警告的情況下呼叫該函式。
語法
util.deprecate( fn, msg, [code] )
引數
引數在下面定義
- fn − 需要標記為已棄用的函式。
- msg − 這是在呼叫已棄用函式時呼叫的警告訊息。
- code − 這是可選引數,用於顯示已棄用函式的傳遞程式碼。
示例 1
建立一個名為 "deprecate.js" 的檔案,並複製以下程式碼段。建立檔案後,使用命令 "node deprecate.js" 來執行此程式碼。
const util = require('util');
var deprecateFunction = util.deprecate(
// Defining the deprecated function
function () { },
// Msg printed for deprecation
"Warning: This method is deprecated !",
// Deprecated API
'Deprication API'
);
// Function call
deprecateFunction();輸出
C:\home
ode>> node deprecate.js (node:153883) [Deprication API] DeprecationWarning: Warning: This method is deprecated !
示例 2
const util = require('util');
function fun() {
console.log("Welcome to Tutorials Point");
}
var msg = 'This function is deprecated'
var code = 'DEP0001';
var deprecateFunction = util.deprecate(fun, msg, code);
// Function call
deprecateFunction();輸出
C:\home
ode>> node deprecate.js Welcome to Tutorials Point (node:157003) [DEP0001] DeprecationWarning: This function is deprecated
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP