jskatas.org Continuously Learn JavaScript. Your Way.

Reflect: Reflect.construct()

The new operator as a function.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

Reflect.construct is the new operator as a function

the function itself

  • is static on the Reflect object
  • is of type function

the 1st parameter is the constructor to be invoked

  • fails when given a number as constructor
  • works given a function that can be instanciated
  • works given a class

the 2nd parameter is a list of arguments, that will be passed to the constructor

  • fails when it`s not an array(-like), e.g. a number
  • works with an array-like object (the length property look up should not throw)
  • works with a real array

in use

  • giving it a class it returns an instance of this class

the list of arguments are passed to the constructor as given

  • if none given, nothing is passed
  • passing an array, all args of any type are passed

the length property

  • of Reflect.construct is 2

Links

How this function is specified.
How the arguments list that can be passed as second parameter is specified.
Axel Rauschmayer explaining in his book "The data flow between class constructors is different from the canonical way of subclassing in ES5."
The chapter on Reflect in the book "Exploring ES6"
Announcement of this kata on twitter.