Call a Dynamic Function with Dynamic Params

Useful for wrapper APIs or communicating with external interfaces: Note: This won't work in a strict mode.

function callFunction(fn, ...rest) {
  this[fn].apply(this, rest)
}
callFunction('alert', 'Ping')
callFunction('setTimeout', () => alert('Timeout after 1s'), 1000)
Spread the word on BlueskyEdit on GithubPublished on Jan 11, 2020

Copyright © 2019...2025 Nick Plekhanov