Last active
March 29, 2024 06:40
-
-
Save ryanmorr/98fee9895080298c32b9d18cd9fe1ee1 to your computer and use it in GitHub Desktop.
Port a method to be used standalone
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 port(fn) { | |
return Function.prototype.bind.call(Function.call, fn); | |
} | |
// Usage: | |
// port the `forEach` method of the Array prototype | |
const each = port([].forEach); | |
// Use standalone by passing the contextual object as the first argument | |
each(document.querySelectorAll('div'), (el) => console.log(el)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment