几个月前,我创建了一个在 Npm 上发布的 Js 库。现在我想重命名一些函数。我读了这篇文章,我认为它非常有用。
假设我有一个文件A:
export function function1(param1, param2) {
return param1 + param2
}
Run Code Online (Sandbox Code Playgroud)
库用户可用的导出函数位于 index.js 文件中:
export { function1 } from './A'
Run Code Online (Sandbox Code Playgroud)
我想将其重命名为sum(param1, param2).
我创建了这个obsolete函数:
function obsolete(newFunction, oldFnName, newFnName) {
const wrapper = function () {
console.warn(
`Obsolete function called. Function '${oldFnName}' has been deprecated, please use the new '${newFnName}' function instead.`
)
newFunction.apply(this, arguments)
}
wrapper.prototype = newFunction.prototype
return wrapper
}
Run Code Online (Sandbox Code Playgroud)
现在我必须做什么?我想我必须以这种方式修改A文件:
/** @deprecated since version 2.0 */
export function function1(param1, param2) {
return sum(param1, param2)
}
export function sum(param1, param2) {
return param1 + param2
}
Run Code Online (Sandbox Code Playgroud)
并将sum函数添加到索引文件中:
export { function1, sum } from './A'
Run Code Online (Sandbox Code Playgroud)
进而?如何使用该obsolete功能?
Lia*_*iam 13
好的,所以这个问题似乎是由您的评论提示的(现在似乎已删除):
假设我有一个函数 fun1,我想让它弃用,而新函数是 fun2。如何使用这个过时的功能?看起来很有趣
从这个问题。
所以首先,你得到一个 JSdoc ( /** @deprecated since version 2.0 */) 注释和这个函数混淆了。
JSDoc 是一种标记语言,用于注释 JavaScript 源代码文件
因此,这仅在您计划创建 JSdoc 注释时有用。我假设你不是。如果您使用 JSdoc 那么这应该按原样工作吗?
所以忽略我将回到你关于这段代码的问题。
查看您可以使用它的代码(不是 100% 确定,因为我没有编写它):
// the function from the previous question
function obsolete(oldFunc, newFunc) {
const wrapper = function() {
console.warn(`WARNING! Obsolete function called. Function ${oldFunc.name} has been deprecated, please use the new ${newFunc.name} function instead!`)
newFunc.apply(this, arguments)
}
wrapper.prototype = newFunc.prototype
return wrapper
}
// this is the function that is the replacement for obsfunc
function newfunc(){
console.log('new called');
}
// this is the function that is being made "obsolete"
function obsfunc(p) {
return obsolete(obsfunc, newfunc)();
}
// when you call the obsolete function it's actually the new function
// that gets triggered and an error is displayed in the console.
obsfunc();Run Code Online (Sandbox Code Playgroud)
在我的例子中,函数有参数和返回值
此代码不支持。在 Js 规范中没有官方的方法来“过时”一个函数。TBH这是IMO 对该问题的正确(现已删除)答案。因此,您需要编写自己的解决方案。也不清楚您对“过时”的定义是什么?装饰器是一个不错的选择
你的函数的用法obsolete非常简单:
您所要做的就是将新函数传递给它:
/** @deprecated since version 2.0 */
export const function1 = obsolete(function function1(param1, param2) {
return param1 + param2
}, 'function1', 'sum')
Run Code Online (Sandbox Code Playgroud)
您可以在声明中添加的另一个小调整obsolete是名称设置,以便过时的函数看起来与原始函数相同(这将避免破坏任何人的代码 -或不破坏任何人的代码?):
function obsolete(newFunction, oldFnName, newFnName) {
const wrapper = function () {
console.warn(
`Obsolete function called. Function '${oldFnName}' has been deprecated, please use the new '${newFnName}' function instead.`
)
newFunction.apply(this, arguments)
}
wrapper.prototype = newFunction.prototype
Object.defineProperty(wrapper, 'name', {
value: oldName,
enumerable: true,
configurable: true
}
return wrapper
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您只是重命名一个方法,则可以通过将新方法的引用传递给以下方法来避免重新定义obsolete:
export function sum(param1, param2) {
return param1 + param2
}
/** @deprecated since version 2.0 */
export const function1 = obsolete(sum, 'function1', 'sum')
Run Code Online (Sandbox Code Playgroud)
通过上面的名称修复,这甚至会将新函数的已弃用版本重命名为其旧名称,而无需手动创建包装器。