jskatas.org Continuously Learn JavaScript. Your Way.

Function constructor: new Function() and Function()

With the Function constructor one can dynamically create a Function object.

Using new Function() creates a new function

WHEN using new Function() THEN this is equivalent to Function() with the same parameters
const fn1 = new Function('a', 'b', 'return a + b'); const fn2 = Function('a', 'b', 'return a + b'); assert.deepStrictEqual(Object.getOwnPropertyDescriptors(fn1), Object.getOwnPropertyDescriptors(fn2));
num params
const fn = new Function('a, b', 'c', 'return null'); assert.strictEqual(fn.length, 3);

Links

The very first version of the spec defines this property already, the ES1 spec, see section 15.3.2.1 (PDF 732kB).
The MDN pages describing this property, easy to read with examples.

Related Katas

function API

Arrow functions

Async Function

Difficulty Level

INTERMEDIATE

Stats

2 tests to solve