util.deprecate()(在 v0.8.0_ 方法中添加是 util 模块的内置应用程序编程接口,它以标记为已弃用的方式包装 fn(可能是函数或类)。当 util.deprecate() 方法被调用时,它返回一个使用 ‘warning’ 事件发出 DeprecationWarning 的函数。第一次调用返回的函数时,发出警告并打印到 stderr。一旦发出警告,则调用包装函数。警告是如果在对“util.deprecate()”的多次调用中提供了相同的可选代码,则仅发出一次。
用法:
util.deprecate(fn, msg, )
参数:该函数接受上述和以下所述的三个参数:
- fn:不推荐使用的是 <function>。
 - msg:调用不推荐使用的函数时,需要显示“警告消息”<string>。
 - code:它是与弃用警告一起打印的弃用代码 <string>。一些已弃用的 API 是 DEP0001、DEP0001、...、DEP0143。
 
返回值:它返回包装以发出警告的弃用函数,即在运行完整函数后,警告消息与弃用的 API 一起返回。
范例1: 文件名:index.js
// Node.js program to demonstrate the 
// util.deprecate() method 
  
// Import the util module 
var util = require('util');
  
var depricateFunction = util.deprecate(
  
    // Function which is depricated
    function () { },
  
    // Warning message that is 
    // printed to stderr
    "someWarningMessage",
  
    // Deprecated API
    'Deprication API'
);
  
// Function call
depricateFunction();使用以下命令运行index.js文件:
node index.js
输出:
[Deprication API] DepricationWarning:someWarningMessage
范例2: 文件名:index.js
// Node.js program to demonstrate the 
// util.deprecate() method 
  
// Import the util module 
var util = require('util');
var outerValue = "along with"
  
var geekyFunction1 = util.deprecate(() => {
    console.log("This Depricated function is working, ");
},
    // The arrow function which is depricated
    "geekyFunction() is deprecated. Use "
        + "geeksForGeeksFunction() instead",
  
    // The warning message that is printed
    // to stderr
    'DEP0014' // Deprecated API
);
  
var geekyFunction2 = util.deprecate(function (call) {
    console.log("This Depricated function is working, ", 
            outerValue, call());
},
    // The function which is depricated
    "geekyFunction() is deprecated. Use "
        + "geeksForGeeksFunction() instead",
    // The warning message that is printed to stderr
    'DEP0014' // Deprecated API
);
  
// Function call
geekyFunction1();
  
// Function call
geekyFunction2(function call() {
    console.log("Callback");
    return "return value"
});使用以下命令运行index.js文件:
node index.js
输出:
This Depricated function is working,
Callback
This Depricated function is working, along with return value
[DEP0014] DeprecationWarning:geekyFunction() is deprecated. Use geeksforgeeksFunction() instead
条件:
- 如果设置了 trace-deprecation/trace-warnings cmd 标志或 process.traceDeprecation 属性设置为 true,则第一次将堆栈跟踪和警告打印到 stderr。
 - 如果设置了 -throw-deprecation cmd 标志,或者 process.throwDeprecation 设置为 true,则会引发异常。
 - 如果 process.noDeprecation 属性设置为 true,或者使用了 no-deprecation/no-warnings cmd 标志,则 util.deprecate() 不执行任何操作。
 - trace-deprecation和 process.traceDeprecation 的优先级低于 -throw-deprecation cmd 标志和 process.throwDeprecation 属性。
 
参考: https://nodejs.org/api/util.html#util_util_deprecate_fn_msg_code
相关用法
- Node.js process.nextTick()用法及代码示例
 - Node.js GM solarize()用法及代码示例
 - Node.js MySQL Max()用法及代码示例
 - Lodash _.method()用法及代码示例
 - Node.js Http2ServerRequest.method用法及代码示例
 - Node.js http.IncomingMessage.method用法及代码示例
 - Collect.js toArray()用法及代码示例
 - Javascript RegExp toString()用法及代码示例
 - Tensorflow.js tf.Sequential.evaluate()用法及代码示例
 - Node.js URLSearchParams.has()用法及代码示例
 - JavaScript Math cosh()用法及代码示例
 - Node.js hmac.update()用法及代码示例
 - jQWidgets jqxFormattedInput val()用法及代码示例
 - HTML DOM isEqualNode()用法及代码示例
 - JavaScript Date toLocaleTimeString()用法及代码示例
 - Tensorflow.js tf.Tensor.buffer()用法及代码示例
 
注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js util.deprecate() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
