-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldConsoleLog.ts
More file actions
30 lines (28 loc) · 870 Bytes
/
HelloWorldConsoleLog.ts
File metadata and controls
30 lines (28 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* hello_world("console.log")
* 2024.05.23
*
* Demonstrates dynamically calling a function by its name,
* passing the name of the currently executing function as an argument.
*
* Note: Using the `new Function` constructor can pose security risks.
*
* @param funcName Name of the function to be called
* @example
* helloWorld("console.log")
* // Output:
* // helloWorld
*/
function helloWorld(funcName: string): void {
/**
* Dynamically calls the given function using its name.
* The name of the current executing function is passed as an argument.
*
* @param funcName Name of the function to be called
*/
const currentFuncName = helloWorld.name;
const func = new Function(`return ${funcName}`)();
func(currentFuncName);
}
// Dynamically executes the console.log function
helloWorld("console.log");