Is it possible to create a JavaScript function with a name determined at runtime, without using eval or unconventional engine-specific features? Yes, this is now possible with ECMAScript 2015 (ES6) and beyond.
In ES6, an anonymous function expression assigned to an object property takes the name of that property. This can be combined with computed property names to name a function without new Function or eval. For example:
This creates a function named "foo###" where ### is a random 1-3 digit number.
The function's name property will reflect the dynamic name. The compatibility note in the code snippet above refers to the fact that Edge and Safari currently do not show the name in stack traces.
The above is the detailed content of Can You Create Runtime-Named Functions in JavaScript Without `eval`?. For more information, please follow other related articles on the PHP Chinese website!