JS: Function Constructor
The Keyword: Function
The keyword Function
(capital F) lets you construct a function from strings.
The advantage of using Function
to define a function is that it lets you create function at run-time. Though, this is rarely needed.
Function(param1, param2, etc, body)
-
Return a function with specified params and body. The arguments should all be strings.
new Function (args)
-
same as
Function (args)
Example
// function with no parameter const ff = new Function("return 3;"); console.log(ff()); // 3
// function with 1 parameter const gg = new Function("a", "return a;"); console.log(gg(4)); // 4
// function with 2 parameters const hh = new Function("a", "b", "return a + b;"); console.log(hh(3, 4)); // 7
JavaScript. Function
- JS: Define Function
- JS: Arrow Function
- JS: Function Parameters
- JS: arguments (object)
- JS: Function Rest Parameters
- JS: Function Parameter Default Value
- JS: Function Argument Destructure
- JS: Function. Declaration vs Expression
- JS: Closure
- JS: Function Call, Apply, Bind
- JS: Functional Programing
- JS: Function Pipe 📜
- JS: Function (class)
- JS: Function Constructor
- JS: Function.prototype