1

In some places, they say that Array, Date and Object are built-in classes. However, I find that they can all be called without new which isn't supposed to be possible with classes.

And even though Map, Set, etc... can't be called without new. All of them will show something like function ItsName() { [native code] } while trying to get their definition while I would think a class is supposed to show something like class ItsName { [native code] }.

4
  • JavaScript has been evolving for a long time. Map and Set are much newer than Array and Date. Commented Feb 5, 2023 at 15:06
  • So because Array and Object are older, they can be called without new and Map and Set can't be called without new because it would be disrepectful to not call them new. Commented Feb 5, 2023 at 15:35
  • 1
    Well it's a little more complicated than that. The older constructor functions have different semantics when called without new, and while it may be a little inelegant it makes sense. It's not clear what sort of behavior Map() would have without new. Note also that Symbol represents a class of items, but it cannot be called with new. In the end, these are things that the standards committee decides, and there's nothing we can do about it. Commented Feb 5, 2023 at 15:52
  • 1
    Note that you can write your own functions to behave such that they insist on being called with new. Commented Feb 5, 2023 at 15:53

1 Answer 1

1

The most specific description for objects like Object, Array, etc. is "built-in constructor".

In short, section 4.4.12 built-in object annotates:

NOTE
Standard built-in objects are defined in this specification. An ECMAScript implementation may specify and supply additional kinds of built-in objects. A built-in constructor is a built-in object that is also a constructor.

Section 4 is non-normative, but the above note can be derived from the following:

  1. Section 6.1.7.2 Object Internal Methods and Internal Slots: (Boldness applied by me)

    ... A function object is an object that supports the [[Call]] internal method. A constructor is an object that supports the [[Construct]] internal method. Every object that supports [[Construct]] must support [[Call]]; that is, every constructor must be a function object. Therefore, a constructor may also be referred to as a constructor function or constructor function object.

    This means, constructors are functions, and functions are objects.

  2. Section 6.1.7.4 Well-Known Intrinsic Objects:

    Well-known intrinsics are built-in objects (...).

    And:

    The well-known intrinsics are listed in Table 6.

    With Table 6:

    Global Name ECMAScript Language Association
    Array The Array constructor (23.1.1)
    Object The Object constructor (20.1.1)
    ... ...

Point 2 lists several built-in objects like Array and Object. The table and the definitions in the corresponding links call these "constructors".

Point 1 explains that constructors are objects, so it is valid to say "built-in constructor" instead of "built-in objects", where applicable.


Here is a short summary of other interesting terms and definitions in the specification:

  • An object is a member of the type Object. (See 4.4.6)
  • A function (or function object) is an object that supports the internal function [[Call]] (which may or may not be exposed). (See 6.1.7.2 Table 5)
  • A constructor (or constructor function or constructor function object) is a function that supports the internal function [[Construct]] (commonly invoked with the new operator). (See 6.1.7.2 Table 5)
  • All constructors have the property prototype. (See 4.3.1)
  • Both function definitions and function expressions create constructors. (See 15.2.4 and 15.2.5, respectively)
  • Arrow function expressions do not create constructors and can therefore not be instantiated. (See 15.3.4)
  • Class definitions (ES6 class syntax) return constructor functions that are uncallable (meaning they do not expose [[Call]]). (See 15.7.14)
Sign up to request clarification or add additional context in comments.

2 Comments

I more or less understand everything you wrote except for internal function [[Construct]] and [[Call]]. What are they?
@Superhuman Much like how classes can have private members that are inaccessible outside the class, objects can have internal members that are inaccessible -to us- in JavaScript (meaning, only accessible to the core engine).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.