Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
Created June 15, 2020 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friendlyanon/68635eb681ff1ffc491ef3e8dc8f6757 to your computer and use it in GitHub Desktop.
Save friendlyanon/68635eb681ff1ffc491ef3e8dc8f6757 to your computer and use it in GitHub Desktop.
function once(fn) {
const { name, length } = fn;
let called = false;
// match .name of argument
const wrapped = {
[name]() {
if (!called) {
called = true;
fn.apply(this, arguments);
}
}
}[name];
// match .length of argument
Object.defineProperty(wrapped, "length", {
value: length,
configurable: true,
});
return wrapped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment