2

I have been diving into JavaScript recently, and after spending several months on it I am still confused about some of the internals.

Specifically, I was trying to wrap my head around the so-called Standard Built-In Objects.

What I Know

  1. All functions in JavaScript, both built-in and user-created, are objects (function objects)
  2. Difference between general objects and function objects is that function objects implement the [[Call]] property (and can thus be invoked)
  3. All function objects implement the .prototype property, which represents the prototype of all objects created with the function object as a constructor

Questions

  1. Are all the Standard Built-In Objects actually function objects (i.e. constructor functions)?
  2. Do all (and only) function objects implement .prototype?
  3. Are the right terms general object vs function object?

Thank you.

2 Answers 2

2

All function objects implement the .prototype property, which represents the prototype of all objects created with the function object as a constructor

No. Only constructor functions do that. There are functions that are no constructors (i.e. they have a [[call]] but no [[construct]] internal method), they throw an error when you try to use them with new (as do some functions that have a [[construct]] internal method, but throw nonetheless).

And technically, not even constructor functions necessarily need a .prototype. You could implement a builtin that can be constructed but does not have a .prototype property. It's true however that all native constructor functions do have one.

Are all the Standard Built-In Objects actually function objects?

No. Consider the Math, JSON, Atomics or Reflect built-in objects. They're not functions at all.

function objects (i.e. constructor functions)?

No. Consider the parseInt, JSON.stringify or Array.prototype.slice functions (and many more globals, static and prototype methods). They're not constructors at all.

Do all (and only) function objects implement .prototype?

No. There is nothing special about the .prototype property, except that it is usually used on a function when constructing an instance with new. But every object can have or can not have a .prototype property.

Are the right terms general object vs function object?

No, "general objects" is not an official term. I'd name them non-callable objects and callable objects if I had to (the latter term is used in the spec).

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Bergi. But only constructor functions have a .prototype property natively? General objects (i.e. not function objects), do not have this property by default right? I guess it only really makes sense for constructor functions, as it is used to set the __proto__ of new objects created with that function.
Also, is general objects the official term for objects that are not function objects?
@Magnus Well {prototype: 123} is a native object that has one but it not a function (constructor or otherwise). True, I don't know of any built-in one that looks like this, but there's nothing that would prevent it.
@Magnus They are internal. Inspecting an object doesn't show them (like many others that the object has). Notice that [[scopes]] and __proto__ are not actual properties either, they are just displayed to represent some internal values.
@Magnus No. And it's not even guaranteed that these actually exist in the engine implementation. They are just used in the specification document do describe how the language should behave.
|
1
  1. No, some built-in functions throw an exception if you try to use them with new (Symbol comes to mind).
  2. The prototype concept only applies to functions, though you can create an object with anything you want as the immediate parent in its prototype chain with Object.create()
  3. All functions are objects, but not all objects are functions.

To clarify 2, all objects have a prototype chain with zero or more objects arranged in (effectively) a list. The automatic setting of a new object's prototype chain based on constructor function .prototype value only applies to functions, but that's sort-of tautological because a function is the only thing that can be a constructor.

3 Comments

Thanks Pointy. Surprising how MDN (and the web in general) lack a good high-level explanation of built-ins and object wrappers, constructor functions and .prototype property vs proto (the prototype chain). Follow-up question regarding point 3: Is it called general objects and function objects? Are those the official terms? And, regarding point 2: The so-called global Object from which all other objects descend (prototype link) is really Object.prototype, not Object, correct? Finally, regarding 1: Symbol is a primitive, but most of the other built-ins are function objects?
Well there's no "official" terminology that I know of for objects versus function objects; generally people use the term "object" when talking/writing about objects in general, and "function" when talking about functions in particular. Functions really are objects, and pretty much anything you can do with a plain object you can also do with a function (add attributes, remove attributes, etc).
The Symbol built-in is indeed a function (and therefore a function object).

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.