Created
February 27, 2013 00:29
-
-
Save valueof/5043724 to your computer and use it in GitHub Desktop.
Destructuring with bound functions, yay/nay?
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 bind(obj) { | |
let bound = {}; | |
for (let key in obj) { | |
bound[key] = typeof obj[key] === "function" ? obj[key].bind(obj) : obj[key]; | |
} | |
return bound; | |
} | |
function A() { | |
this.two = 2; | |
} | |
A.prototype.one = function () { | |
return this.two - 1; | |
}; | |
A.prototype.sup = function () { | |
let {one, two} = bind(this); | |
return one() + two; | |
}; | |
let a = new A(); | |
a.sup(); // --> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The side effects are going to be sad when dealing with getters, since they'll be executed when you do typeof obj[key] === "function".
getOwnPropertyDescriptor all the things.