Created
November 13, 2014 21:15
-
-
Save designfrontier/f1644994ae8c10c1bd74 to your computer and use it in GitHub Desktop.
interview.js solution with modern schtuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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