
- TypeScript - Home
- TypeScript - Roadmap
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript vs. JavaScript
- TypeScript - Features
- TypeScript - Variables
- TypeScript - let & const
- TypeScript - Operators
- TypeScript - Types
- TypeScript - Type Annotations
- TypeScript - Type Inference
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Boolean
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Enums
- TypeScript - Any
- TypeScript - Never
- TypeScript - Union
- TypeScript - Literal Types
- TypeScript - Symbols
- TypeScript - null vs. undefined
- TypeScript - Type Aliases
- TypeScript Control Flow
- TypeScript - Decision Making
- TypeScript - If Statement
- TypeScript - If Else Statement
- TypeScript - Nested If Statements
- TypeScript - Switch Statement
- TypeScript - Loops
- TypeScript - For Loop
- TypeScript - While Loop
- TypeScript - Do While Loop
- TypeScript Functions
- TypeScript - Functions
- TypeScript - Function Types
- TypeScript - Optional Parameters
- TypeScript - Default Parameters
- TypeScript - Anonymous Functions
- TypeScript - Function Constructor
- TypeScript - Rest Parameter
- TypeScript - Parameter Destructuring
- TypeScript - Arrow Functions
- TypeScript Interfaces
- TypeScript - Interfaces
- TypeScript - Extending Interfaces
- TypeScript Classes and Objects
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Access Modifiers
- TypeScript - Readonly Properties
- TypeScript - Inheritance
- TypeScript - Static Methods and Properties
- TypeScript - Abstract Classes
- TypeScript - Accessors
- TypeScript - Duck-Typing
- TypeScript Advanced Types
- TypeScript - Intersection Types
- TypeScript - Type Guards
- TypeScript - Type Assertions
- TypeScript Type Manipulation
- TypeScript - Creating Types from Types
- TypeScript - Keyof Type Operator
- TypeScript - Typeof Type Operator
- TypeScript - Indexed Access Types
- TypeScript - Conditional Types
- TypeScript - Mapped Types
- TypeScript - Template Literal Types
- TypeScript Generics
- TypeScript - Generics
- TypeScript - Generic Constraints
- TypeScript - Generic Interfaces
- TypeScript - Generic Classes
- TypeScript Miscellaneous
- TypeScript - Triple-Slash Directives
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript - Decorators
- TypeScript - Type Compatibility
- TypeScript - Date Object
- TypeScript - Iterators and Generators
- TypeScript - Mixins
- TypeScript - Utility Types
- TypeScript - Boxing and Unboxing
- TypeScript - tsconfig.json
- From JavaScript To TypeScript
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Cheatsheet
- TypeScript - Useful Resources
- TypeScript - Discussion
TypeScript - The Function () Constructor
The Function() Constructor
TypeScript supports the built-in JavaScript constructor called Function() to defined a function. The Function() constructor dynamically creates a function object at runtime.
You can define your function dynamically using Function() constructor along with the new operator.
The Function() constructor can accept multiple arguments. All the arguments except the last one are names of parameters and the last argument is the function body of new function to be created.
Syntax
Following is the syntax to define a function using Function constructor along with new operator −
let res = new Function(arg1, arg2, ..., functionBody); let res = Function(arg1, arg2, ..., functionBody);
Function() can be called with or without new. Both syntaxes will create a new Function instance.
All the arguments, i.e., arg1, arg2, ..., functionBody, are strings.
Arguments
arg1, arg2, ..., - These are optional arguments treated as the names of the parameters in the function to be created.
functionBody − This argument contains the statements in function definition of the new function to be created.
All the arguments except the last one are optional. The last argument is required. If you are passing only a single argument, then it will be treated as function body.
Notice that the Function() constructor is not passed any argument that specifies a name for the function it creates. The unnamed functions created with the Function() constructor are called anonymous functions.
The new Function() is a call to the constructor which in turn creates and returns a function reference.
Examples
Let's understand the Function constructor with help of some example programs in TypeScript.
Example 1: Creating a simple function without parameters
In the example below, the Function() constructor takes only single argument. This argument is treated as the function body.
const greet = new Function("return 'Welcome to Tutorials Point!'"); console.log(greet());
On compiling TypeScript generate the same code in JavaScript.
The output of the above example code is as follows −
Welcome to Tutorials Point!
Example 2: Creating a simple function with parameters
In the example below, we call the Function() constructor passing three arguments, "x", "y" and "return x + y". The first two arguments are the names of the parameters of the new function instance, i.e., resFunction.
const resFucntion = new Function("x", "y", "return x + y"); let sum = resFucntion(5, 10); console.log(sum);
On compiling, TypeScript will generate the same code in JavaScript.
The compiled JavaScript code will produce the following output −
15
Example 3: Creating a function instance from a function expression
In the example below, we define a function sum with function expression and pass it to the Function() constructor as a part of the parameter (function body).
Here the function expression requires a return statement with the function's name.
const add = new Function( "const sum = function (a, b) {return a+ b}; return sum", )(); console.log(add(2,3));
TypeScript compiler will generate the same code in JavaScript.
The JavaScript code will produce the following output −
5
Example 4: Creating a function instance from a function declaration
In the example below, we pass a function declaration as an argument to the Function constructor. The function declaration doesnt need a return statement with the function' name.
const sayHello = new Function( "return function (name) { return `Hello, ${name}` }", )(); console.log(sayHello("Shahid"));
On compiling, it will generate the same code in JavaScript.
The output of the above example code is as follows −
Hello Shahid
The Function constructor in TypeScript can be used to define a function at execution time, but you should use it with caution as it can lead to vulnerabilities in the code.