Obsolete helper function

Creating a deprecated / obsolete behavior for methods in a library.

by Konstantin Rouda

JavaScript

// obsolete 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!");
        debugger;
        replacementFunction.apply(this, arguments);
    }
    wrapper.prototype = replacementFunction.prototype;
    
    return wrapper;
}

// new function implementation
var Chart = function(name, data) {
    this.name = name;
    this.data = data;
    
    console.log("Chart > constructor");
}

Chart.prototype.draw = function() {
    console.log("Chart > draw", this.name, this.data);
}

// old deprecated function
var chart = ObsoleteWithReplacement(Chart, "chart", "Chart");

// when a dev calls the old implentation, it invokes the new implementation but displays a warning...
var myChart = new chart("Hello", [1, 2]);
myChart.draw();