Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a function as deprecated

Tags:

javascript

Some months ago I created a Js library that I published on Npm. Now I would like to rename some functions. I read this post and I think it's very useful.

Suppose I have a file A:

export function function1(param1, param2) {
  return param1 + param2
}

The exported functions that are usable from library users are in index.js file:

export { function1 } from './A'

and I want to rename it as sum(param1, param2).

I create this obsolete function:

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
}

Now what I have to do? I suppose I have to modify the A file in this way:

/** @deprecated since version 2.0 */
export function function1(param1, param2) {
  return sum(param1, param2)
}

export function sum(param1, param2) {
  return param1 + param2
}

and add the sum function to the index file:

export { function1, sum } from './A'

And then? How can I use the obsolete function?

like image 341
whitecircle Avatar asked Nov 08 '20 13:11

whitecircle


People also ask

How do you make a method deprecated?

Using the @Deprecated Annotation To use it, you simply precede the class, method, or member declaration with "@Deprecated." Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element.

How do you make a function deprecated in Python?

To mark a function or method as deprecated, wrap it in the deprecated() decorator. This does several things for you: The docstring of the wrapped function will have details appended to it from the arguments you set on deprecated() .


Video Answer


1 Answers

Ok, so this question seems to have been prompted by your comment (now deleted it seems):

Suppose I have a function fun1 that I want to make it deprecated and the new function is fun2. How can I use this obsolete function? It seems interesting

From this question.


So firstly, your getting a JSdoc (/** @deprecated since version 2.0 */) annotation and this function mixed up.

JSDoc is a markup language used to annotate JavaScript source code files

source

So this is only useful if your planning on creating a JSdoc annotation. Which I'm presuming your not. If your using JSdoc then this should work as is?


So ignoring that I'm going to go back to your question on this code.

Looking at the code you could use it like (not 100% sure as I didn't write it):

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

in my case functions have parameters and a return value

this code doesn't support that. There is no official way to "obsolete" a function in the Js spec. TBH this is the correct (now deleted) answer to that question IMO. So you'll need to write your own solution. It's not really clear what your definition of "obsolete" is either? Decorators is a good option

like image 122
Liam Avatar answered Oct 15 '22 05:10

Liam