Created
October 18, 2017 23:24
-
-
Save dounan/9e9ed351eeee858f84d7ecd759d59ae8 to your computer and use it in GitHub Desktop.
reflective-bind-implementation
This file contains 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 reflectiveBind(f, ctx, ...args) { | |
const result = f.bind(ctx, ...args); | |
Object.defineProperty(result, "__func", {value: f}); | |
Object.defineProperty(result, "__ctx", {value: ctx}); | |
Object.defineProperty(result, "__args", {value: args}); | |
return result; | |
} | |
function reflectiveEqual(f, g) { | |
return ( | |
isReflective(f) && | |
isReflective(g) && | |
f.__ctx === g.__ctx && | |
arrayShallowEq(f.__args, g.__args) && | |
(f.__func === g.__func || reflectiveEqual(f.__func, g.__func)) | |
); | |
} | |
function isReflective(f) { | |
return ( | |
typeof f === "function" && | |
typeof f.__func === "function" && | |
f.hasOwnProperty("__ctx") && | |
Array.isArray(f.__args) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment