Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Created November 13, 2014 21:15
Show Gist options
  • Save designfrontier/f1644994ae8c10c1bd74 to your computer and use it in GitHub Desktop.
Save designfrontier/f1644994ae8c10c1bd74 to your computer and use it in GitHub Desktop.
interview.js solution with modern schtuff
function delegate (childIn) {
var child = childIn;
return function me(){
var that = this,
oper = Object.getOwnPropertyNames(that).filter(function (attrib) {
return(typeof that[child][attrib] === 'function' && me === that[attrib]);
})[0];
return that[child][oper].apply(this[child], arguments);
};
}
// usage
var obj = {
math: {
x: 2,
add: function (y) {
return this.x + y;
},
multiply: function (y) {
return this.x * y;
}
},
add: delegate('math'),
multiply: delegate('math')
};
// test cases
console.assert( obj.add(1) === 3 );
console.assert( obj.multiply(3) === 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment