util.deprecate(fn, msg[, code])
-
fn
<Function> 被弃用的函数。¥
fn
<Function> The function that is being deprecated. -
msg
<string> 调用弃用的函数时显示的警告消息。¥
msg
<string> A warning message to display when the deprecated function is invoked. -
code
<string> 弃用代码。有关代码列表,请参见 弃用的 API 列表。¥
code
<string> A deprecation code. See the list of deprecated APIs for a list of codes. -
返回:<Function> 弃用的函数被封装以触发警告。
¥Returns: <Function> The deprecated function wrapped to emit a warning.
util.deprecate()
方法以标记为已弃用的方式封装 fn
(可能是函数或类)。
¥The util.deprecate()
method wraps fn
(which may be a function or class) in
such a way that it is marked as deprecated.
import { deprecate } from 'node:util';
export const obsoleteFunction = deprecate(() => {
// Do something here.
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
const { deprecate } = require('node:util');
exports.obsoleteFunction = deprecate(() => {
// Do something here.
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
当调用时,util.deprecate()
将返回将使用 'warning'
事件触发 DeprecationWarning
的函数。第一次调用返回的函数时,警告将触发并打印到 stderr
。触发警告后,将调用封装的函数而不触发警告。
¥When called, util.deprecate()
will return a function that will emit a
DeprecationWarning
using the 'warning'
event. The warning will
be emitted and printed to stderr
the first time the returned function is
called. After the warning is emitted, the wrapped function is called without
emitting a warning.
如果在多次调用 util.deprecate()
时提供了相同的可选 code
,则该 code
只会触发一次警告。
¥If the same optional code
is supplied in multiple calls to util.deprecate()
,
the warning will be emitted only once for that code
.
import { deprecate } from 'node:util';
const fn1 = deprecate(
() => 'a value',
'deprecation message',
'DEP0001',
);
const fn2 = deprecate(
() => 'a different value',
'other dep message',
'DEP0001',
);
fn1(); // Emits a deprecation warning with code DEP0001
fn2(); // Does not emit a deprecation warning because it has the same code
const { deprecate } = require('node:util');
const fn1 = deprecate(
function() {
return 'a value';
},
'deprecation message',
'DEP0001',
);
const fn2 = deprecate(
function() {
return 'a different value';
},
'other dep message',
'DEP0001',
);
fn1(); // Emits a deprecation warning with code DEP0001
fn2(); // Does not emit a deprecation warning because it has the same code
如果使用 --no-deprecation
或 --no-warnings
命令行标志,或者如果在第一次弃用警告之前将 process.noDeprecation
属性设置为 true
,则 util.deprecate()
方法不执行任何操作。
¥If either the --no-deprecation
or --no-warnings
command-line flags are
used, or if the process.noDeprecation
property is set to true
prior to
the first deprecation warning, the util.deprecate()
method does nothing.
如果设置了 --trace-deprecation
或 --trace-warnings
命令行标志、或者 process.traceDeprecation
属性设置为 true
,则在第一次调用已弃用的函数时会向 stderr
打印警告和堆栈跟踪。
¥If the --trace-deprecation
or --trace-warnings
command-line flags are set,
or the process.traceDeprecation
property is set to true
, a warning and a
stack trace are printed to stderr
the first time the deprecated function is
called.
如果设置了 --throw-deprecation
命令行标志、或者 process.throwDeprecation
属性设置为 true
,则在调用已弃用的函数时将抛出异常。
¥If the --throw-deprecation
command-line flag is set, or the
process.throwDeprecation
property is set to true
, then an exception will be
thrown when the deprecated function is called.
--throw-deprecation
命令行标志和 process.throwDeprecation
属性优先于 --trace-deprecation
和 process.traceDeprecation
。
¥The --throw-deprecation
command-line flag and process.throwDeprecation
property take precedence over --trace-deprecation
and
process.traceDeprecation
.