patternjavascriptMinor
Creating a Deprecated/Obsolete behavior for methods in a JavaScript library
Viewed 0 times
obsoletecreatingjavascriptbehaviordeprecatedmethodsforlibrary
Problem
A friend of mine has a popular open source JavaScript library which is quite well used in the community. He is now going through a process of refactoring and applying best practices and conventions and is wanting to upper case all constructor functions. So in the past where he may have defined a
Because the library is well used, he would rather warn devs when they use the old function names and eventually deprecate them.
I've recommended using a helper function :
Which could be called as follows : http://jsfiddle.net/rLNep/1/
Is this a good way to go about it? Can you recommend a better way?
chart object, he now wants it to be called Chart to stick with convention.Because the library is well used, he would rather warn devs when they use the old function names and eventually deprecate them.
I've recommended using a helper function :
var ObsoleteWithReplacement = function(replacementFunction, oldFnName, newFnName) {
var wrapper = function() {
console.warn("WARNING! Obsolete function called. Function '" + oldFnName + "' has been deprecated, please use the new '" + newFnName + "' function instead!");
replacementFunction.apply(this, arguments);
}
wrapper.prototype = replacementFunction.prototype;
return wrapper;
}Which could be called as follows : http://jsfiddle.net/rLNep/1/
Is this a good way to go about it? Can you recommend a better way?
Solution
From looking at your fiddle, the first thing I would do is make sure that your function is not anonymous:
Then, to keep it KISS, I would
Because the function is no longer anonymous, you can now use
Then you can do this:
The name
I forked this to http://jsfiddle.net/konijn_gmail_com/8a5Ax/
// new function implementation
var Chart = function Chart(name, data) { //See what I did there?
this.name = name;
this.data = data;
console.log("Chart > constructor");
}Then, to keep it KISS, I would
// obsolete helper function
var ObsoleteWithReplacement = function( f ) {
var wrapper = function() {
console.warn("This function is now called " + f.name );
f.apply(this, arguments);
}
wrapper.prototype = f.prototype;
return wrapper;
}Because the function is no longer anonymous, you can now use
f.name, you no longer have to provide it. The drawback is that the new old function name is not logged, I think any JS developer could figure that one out.Then you can do this:
// old deprecated function
var chart = ObsoleteWithReplacement(Chart);The name
ObsoleteWithReplacement is unfortunate, I would use simply Obsolete. Also I think function Obsolete(){..} makes more sense than var Obsolete = function Obsolete(){..}, var Obsolete = function (){..} should be avoided since it creates an anonymous function.I forked this to http://jsfiddle.net/konijn_gmail_com/8a5Ax/
Code Snippets
// new function implementation
var Chart = function Chart(name, data) { //See what I did there?
this.name = name;
this.data = data;
console.log("Chart > constructor");
}// obsolete helper function
var ObsoleteWithReplacement = function( f ) {
var wrapper = function() {
console.warn("This function is now called " + f.name );
f.apply(this, arguments);
}
wrapper.prototype = f.prototype;
return wrapper;
}// old deprecated function
var chart = ObsoleteWithReplacement(Chart);Context
StackExchange Code Review Q#41467, answer score: 5
Revisions (0)
No revisions yet.