Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I mark a method as "obsolete" in JS?

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?

like image 338
JBG Avatar asked Oct 16 '13 20:10

JBG


People also ask

What does deprecated mean in JS?

The JavaScript warning "expression closures are deprecated" occurs when the non-standard expression closure syntax (shorthand function syntax) is used. This syntax is now removed and the warning message is obsolete.

Can we use deprecated methods in JavaScript?

These deprecated features can still be used, but should be used with caution because they are expected to be removed entirely sometime in the future. You should work to remove their use from your code.


1 Answers

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!");   bar(); } 
like image 63
Jordão Avatar answered Sep 26 '22 23:09

Jordão