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