Home > Web Front-end > JS Tutorial > Can You Create Runtime-Named Functions in JavaScript Without `eval`?

Can You Create Runtime-Named Functions in JavaScript Without `eval`?

Linda Hamilton
Release: 2024-11-12 18:21:02
Original
956 people have browsed it

Can You Create Runtime-Named Functions in JavaScript Without `eval`?

Runtime-Named Functions without Eval in JavaScript

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.

ES6 Solution: Computed Property Names

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.

Function Name Visibility

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template