我应如何将方法标记为

我应如何将方法标记为

本文介绍了我应如何将方法标记为“过时”?在JS中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一个相当大的JS文件,其中包含许多不相关的方法,可以根据它们的用法将这些方法重新组合在一起,并根据需要重命名其中的一些(以防止误导名称)。



但是,大多数实际使用此代码的网页分散在不同的代码分支中,这使我无法进行简单的查找和替换。我可以在所有不同的分支机构中执行此操作,但是这需要同时在30个以上的分支机构中进行维护,或者一旦更改合并到其他分支机构中(由我或其他团队成员),可能会忘记执行重命名。 / p>

如果这是C#,我可以仅用[作废]标记该方法,它将根据需要标记必要的更改,因此我正在寻找某种等效的东西。通过将调用重定向到新方法,我仍然会在一段时间内为旧界面提供功能,但是我想迫使人们出于其他原因在页面上工作时切换到新界面。



除了添加 debugger;
语句和冗长的注释外,还有其他方法可以做类似的事情

解决方案

在每种方法中都可能发生什么事情?

解决方案

  1. 添加 @已弃用 标志。

  2. 添加一条控制台警告消息,指示该功能已被弃用。

一个样本:

  / ** 
* @自1.0版起不推荐使用。将在3.0版中删除。请改用bar。
* /
function foo(){
console.warn(正在调用不赞成使用的函数!); // TODO:使此跨浏览器
bar();
}


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).

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).

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.

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?

解决方案

There are a couple of things you can do in a transition period.

  1. Add the @deprecated JSDoc flag.
  2. Add a console warning message that indicates that the function is deprecated.

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();
}

这篇关于我应如何将方法标记为“过时”?在JS中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:40