Hướng dẫn javascript dynamic function name
The syntax
So,
displays This method explicitly populates the environment with
displays
and
Though the property function
displays only
The following is not necessarily problematic,
and so is much more consistent with the original problem context. Given these considerations,
maintains the global computing environment with the semantics and syntax of the OP example as much as possible. Say you're writing some higher order functions that return other functions. In my case it was a utility that creates a typed hook to consume some context. I wanted to let the consumer of this utility to have a reasonable
The first attempt did not work.
Enter fullscreen mode Exit fullscreen mode You get a nice error at least. So then I think to myself 🤔 Do I know anything about dynamic naming in general in JavaScript? 💡 Objects can have dynamic keys! With square brackets, we can stringify runtime values.
Enter fullscreen mode Exit fullscreen mode So, to solve my configurable function name problem, I used computed keys to create an object and computed keys again to destructure the function value. 🤯 Right?
Enter fullscreen mode Exit fullscreen mode ^ You can try that example in your browser console. Pretty neat stuff! Here was my final solution, inspired by this cheatsheet.
Enter fullscreen mode Exit fullscreen mode |