Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Last active August 29, 2015 14:06
Show Gist options
  • Save rodneyrehm/f73ed7342ef98f35e3f2 to your computer and use it in GitHub Desktop.
Save rodneyrehm/f73ed7342ef98f35e3f2 to your computer and use it in GitHub Desktop.
JavaScript: providing a backward compatibility layer for changed function names/signatures
/*
Figuring out how to provide a Backward Compatibility Layer
GOALS:
[aliasing] provide a function (nextGeneration) in another name (previousGeneration)
[parameters] the functions might differ in argument order (or length, …)
[warnings] log each access/mutation to the alias (previousGeneration) so old code bases can be cleaned up
*/
var myObject = {
nextGeneration: function(first, second) {
console.log('nextGeneration executed!', first, second);
}
// the same function in an older version:
// previousGeneration: function(second, first) {
// console.log('previousGeneration executed!', first, second);
// }
};
Object.defineProperty(myObject, 'previousGeneration', {
enumerable: true,
configurable: true,
get: function() {
console.warn('the property previousGeneration was replaced by nextGeneration');
return function(second, first) {
console.warn('previousGeneration(second, first) changed to nextGeneration(first, second)');
return this.nextGeneration(first, second);
};
},
set: function(value) {
console.warn('the property previousGeneration was replaced by nextGeneration (both have been updated!)');
Object.defineProperty(this, 'previousGeneration', {
enumerable: true,
writable: true,
value: value
});
this.nextGeneration = function(first, second) {
return this.previousGeneration(second, first);
};
}
});
// testing
console.log('--------------------------------');
console.log('checking existence', !!myObject.nextGeneration, !!myObject.previousGeneration);
console.log('--------------------------------');
console.log('calling nextGeneration()');
myObject.nextGeneration('alpha', 'bravo');
console.log('--------------------------------');
console.log('calling previousGeneration()');
myObject.previousGeneration('bravo', 'alpha');
console.log('--------------------------------');
console.log('overwriting nextGeneration()');
myObject.nextGeneration = function(first, second) {
console.log('new next()', first, second);
};
console.log('--------------------------------');
console.log('calling nextGeneration()');
myObject.nextGeneration('alpha', 'bravo');
console.log('--------------------------------');
console.log('calling previousGeneration()');
myObject.previousGeneration('bravo', 'alpha');
console.log('--------------------------------');
console.log('overwriting previousGeneration()');
myObject.previousGeneration = function(second, first) {
console.log('new previous()', first, second);
};
console.log('--------------------------------');
console.log('calling nextGeneration()');
myObject.nextGeneration('alpha', 'bravo');
console.log('--------------------------------');
console.log('calling previousGeneration()');
myObject.previousGeneration('bravo', 'alpha');
/*
OUTPUT:
--------------------------------
warning: the property previousGeneration was replaced by nextGeneration
checking existence true true
--------------------------------
calling nextGeneration()
nextGeneration executed! alpha bravo
--------------------------------
calling previousGeneration()
warning: the property previousGeneration was replaced by nextGeneration
warning: previousGeneration(second, first) changed to nextGeneration(first, second)
nextGeneration executed! alpha bravo
--------------------------------
overwriting nextGeneration()
--------------------------------
calling nextGeneration()
new next() alpha bravo
--------------------------------
calling previousGeneration()
warning: the property previousGeneration was replaced by nextGeneration
warning: previousGeneration(second, first) changed to nextGeneration(first, second)
new next() alpha bravo
--------------------------------
overwriting previousGeneration()
warning: the property previousGeneration was replaced by nextGeneration (both have been updated!)
--------------------------------
calling nextGeneration()
new previous() alpha bravo
--------------------------------
calling previousGeneration()
new previous() alpha bravo
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment