Skip to content

Instantly share code, notes, and snippets.

@dounan
Created October 18, 2017 23:24
Show Gist options
  • Save dounan/9e9ed351eeee858f84d7ecd759d59ae8 to your computer and use it in GitHub Desktop.
Save dounan/9e9ed351eeee858f84d7ecd759d59ae8 to your computer and use it in GitHub Desktop.
reflective-bind-implementation
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