javascript 我应该如何在 JS 中将方法标记为“过时”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19412660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How should I mark a method as "obsolete" in JS?
提问by JBG
I am refactoring a rather large JS file that contains many unrelated methods into something that will regroup the methods together according to their usage, and renaming some of them as needed (to prevent misleading names).
我正在重构一个相当大的 JS 文件,其中包含许多不相关的方法,以便根据它们的用法将这些方法重新组合在一起,并根据需要重命名其中的一些(以防止误导性名称)。
However, most of the web pages that actually use this code are spread across different code branches, preventing me from doing a simple find&replace. I could do it in all the different branches, but that requires doing maintenance in 30+ branches at the same time, or probably forgetting to perform the renaming once the change is merged in the other branches (by me or other team members).
但是,实际使用此代码的大多数网页都分布在不同的代码分支中,这使我无法进行简单的查找和替换。我可以在所有不同的分支中执行此操作,但这需要同时在 30 多个分支中进行维护,或者在更改合并到其他分支(由我或其他团队成员)后可能忘记执行重命名。
If this was C#, I could just mark the method with [Obsolete] and it would flag the necessary changes as needed, so I am looking for something somewhat equivalent. I will still provide functionality with the old interface for a while by just redirecting the calls to the new methods, but I'd like to "force" people to switch to the new interface as they work on the pages for other reasons.
如果这是 C#,我可以只用 [Obsolete] 标记该方法,它会根据需要标记必要的更改,所以我正在寻找某种等效的东西。我仍然会在一段时间内通过将调用重定向到新方法来提供旧界面的功能,但我想“强制”人们在他们出于其他原因在页面上工作时切换到新界面。
Is there any other way to do something similar, besides adding a debugger;
statement and a verbose comment to every method so that it breaks when developing but not in production?
除了debugger;
在每个方法中添加一个语句和一个详细的注释以使其在开发时而不是在生产中中断之外,还有其他方法可以做类似的事情吗?
回答by Jord?o
There are a couple of things you can do in a transition period.
在过渡期间,您可以做几件事。
- Add the
@deprecated
JSDocflag. - Add a console warning message that indicates that the function is deprecated.
- 添加
@deprecated
JSDoc标志。 - 添加指示该函数已弃用的控制台警告消息。
A sample:
一个样品:
/**
* @deprecated Since version 1.0. Will be deleted in version 3.0. Use bar instead.
*/
function foo() {
console.warn("Calling deprecated function!"); // TODO: make this cross-browser
bar();
}
回答by JBG
Here's what we've found for Visual Studio 2013 : http://msdn.microsoft.com/en-us/library/vstudio/dn387587.aspx
以下是我们为 Visual Studio 2013 找到的内容:http: //msdn.microsoft.com/en-us/library/vstudio/dn387587.aspx
It's not tested yet as we haven't made the switch, but it looks promising.
它还没有经过测试,因为我们还没有进行转换,但它看起来很有希望。
In the meantime, I am inserting a flag at page load depending on context such as :
同时,我根据上下文在页面加载时插入一个标志,例如:
<%
#if DEBUG
Response.Write("<script type=\"text/javascript\"> Flags.Debug = true; </script>");
#endif
%>
and then I call a method that throws an error if the flag is true, or redirect to the new call if it is in release configuration.
然后我调用一个方法,如果标志为真则抛出错误,或者如果它处于发布配置中,则重定向到新调用。
回答by Paul van Dyk
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
}