Ecma262: Fundamental built-in objects as constructor - What are the required steps?

Created on 22 Feb 2019  路  7Comments  路  Source: tc39/ecma262

Taking into account the fundamental built-in objects when used as constructors the specification says:

creates and initializes a new XYZ object when called as a constructor

IMHO, this sentence is too vague:

  1. how is the object created? I think that a more rigid description, in terms of numbered steps and operation calls, should be given
  2. does the constructor return something? The sentence says "creates and initializes", it doesn't explicitly says "and return the created object". I think addition informations should be provided, here too

Most helpful comment

The "creates and initializes a new X object" portion is a non-normative note giving a human-readable summary. The actual steps taken when invoking these are given in the sections immediate following the non-normative notes.

For example, the steps taken when invoking the Number constructor are given in 20.1.1.1.

All 7 comments

What text would you suggest?

@ljharb I would suggest something like this:

When the XYZ object is used as a constructor the following steps are taken:

  1. Let _O_ be the object returned calling ObjectCreate(%XYZPrototype%)
  2. Set property [[property1]] of _O_ to "value1"
  3. Set property [[property2]] of _O_ to "value2"
  4. return _O_

The "creates and initializes a new X object" portion is a non-normative note giving a human-readable summary. The actual steps taken when invoking these are given in the sections immediate following the non-normative notes.

For example, the steps taken when invoking the Number constructor are given in 20.1.1.1.

But it is not clear that what is described in 20.1.1.1 is also used when using the Number object as costructor. Infact here the specs says:

A Number object can be coerced to a number value by calling the Number constructor as a
function (20.1.1.1).

Meaning that 20.1.1.1 describes the Number object when NOT used as constructor.

No, 20.1.1.1 describes its behavior when invoked as a constructor as well. It branches in line 3, the NewTarget is undefined case is when it is invoked as a function.

Ok, got it. Thank you all.

More generally (and explicitly), 9.3 Built-in Function Objects says: "The behaviour specified for each built-in function via algorithm steps or other means is the specification of the function body behaviour for both [[Call]] and [[Construct]] invocations of the function."

Was this page helpful?
0 / 5 - 0 ratings