Rule: function-constructor

Prevents using the built-in Function constructor.

Rationale

Calling the constructor directly is similar to eval, which is a symptom of design issues. String inputs don’t receive type checking and can cause performance issues, particularly when dynamically created.

If you need to dynamically create functions, use “factory” functions that themselves return functions.

Config

Not configurable.

Config examples
"function-constructor": true
Schema
null

Code examples:

Use inline lambdas instead of calling Function
"rules": { "function-constructor": true }
Passes
let doesNothing = () => {};
Fails
let doesNothing = new Function();
Use parameters instead of constructor strings
"rules": { "function-constructor": true }
Passes
let addNumbers = (a, b) => a + b;
Fails
let addNumbers = new Function("a", "b", "return a + b");