3

Within the Terms and Definitions section of ECMAScript 2021 Language Specification, an ordinary object is defined as:

object that has the default behaviour for the essential internal methods that must be supported by all objects

A standard object is defined as:

object whose semantics are defined by this specification

After reading both definitions, I immediately asked myself, "Isn't the default behavior of the essential internal methods that must be supported by all objects also defined in this specification?"

I've tried searching the specification for both terms, but there are over 100 matches for 'ordinary object' and only a handful of references for 'standard object', which didn't provide additional context that made the distinction between these terms clear to me. I've also tried Google searching and none of the results seem relevant to my question.

What is the difference between an ordinary object and a standard object? What is an example of a scenario in which it is useful to distinguish between between these two types of objects?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
pawooten
  • 140
  • 1
  • 9
  • 1
    I'm pretty sure that "standard" objects are things like Date, Boolean, String, etc. The category "standard object" is not identical to "builtin object" because, as the spec says, a given implementation may provide for builtin objects that are *not* from the spec's list of standard builtin objects, but which are not *exotic* objects. – Pointy Jan 02 '22 at 16:04
  • Also this is not something that a day-to-day JavaScript programmer would typically be concerned with. It's interesting to a language implementor, perhaps, but it's not something anybody would think about while making something happen in a Node server process or on a web page. – Pointy Jan 02 '22 at 16:05
  • 1
    Some of the standard objects are _not_ ordinary, e.g. an [array](https://262.ecma-international.org/12.0/#sec-array-exotic-objects) has semantics defined by the spec but does not use the default behaviour for all essential internal methods. – jonrsharpe Jan 02 '22 at 16:10
  • @Pointy Great point about this likely being outside the concern of most developers. I think I follow your other point as well: "standard" objects are required for the implementation to be ECMAScript compliant. An implementation could feature it's own "built-in" objects in addition to the standard ones. – pawooten Jan 02 '22 at 16:25
  • @jonrsharpe yes, quite unusually the spec is extremely vague about the topic. – Pointy Jan 02 '22 at 16:28
  • I *think* it's true that a "standard" object is not exotic, and is *probably* a "builtin" object, but again, the spec has nothing more than those vague one-sentence descriptions. – Pointy Jan 02 '22 at 16:30
  • 1
    @Pointy a _standard_ object **can** be _exotic_ (non-_ordinary_), as in the example I gave above. All standard objects must be built in to be spec-compliant, but not all built in objects must be the standard objects (consider those defined by Node, WHATWG, etc.). – jonrsharpe Jan 02 '22 at 16:34
  • @jonrsharpe well I admire your ability to infer all that from the extremely anemic descriptions in the spec :) – Pointy Jan 02 '22 at 16:43

2 Answers2

2

Yes, every ordinary object is also a standard object.

The term ordinary object is contrasted with an exotic object, which does have non-default implementations of the internal object methods. Examples of standard exotic objects are proxies, arrays and bound functions.

The term standard object (previously also known as "native object", which led to a lot of confusion) is contrasted with a host object or implementation-defined object, which are specified by a different specification (e.g. HTML5) or implemented in the engine without a formal specification. These can be functions (that are otherwise ordinary) with a host-defined call behaviour (e.g. console.log, setTimeout etc) or totally exotic objects (document.all, nodelists, etc) - see web APIs for an overview of browser-defined objects. Other environments will provide other host-defined objects.

Ordinary objects (with standard, default behavior) whose existence is governed by the host implementation or non-ECMAScript standard may be classified as either, I guess.

Just for reference, the last distinction that is relevant for this terminology discussion is between built-in objects (also builtins) and user-defined objects. The former are created by the implementation at startup, providing the environment that user code can interact with. The latter are constructed by user code at runtime. It does unfortunately become a grey area when exotic objects are constructed by native (non-user) code, these are sometimes called "built-in" as well.

See also What is the difference between native objects and host objects? and In ECMAScript, how are some of native objects also built-in?.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1
 const user = { firstname: "Jonas" };

That's an ordinary object (as it is not exotic), however it is not a standard object, as the semantics are defined by me and not by the specification. Its behavior, however, is specified (e.g. user.firstname will evaluate to "Jonas").

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I disagree, this is certainly a standard object - its semantics (what code can do with it and how it behaves) are defined by the ECMAScript standard – Bergi Jan 02 '22 at 18:32
  • @bergi then why does the spec distinguish between "semantics" and "behavior"? – Jonas Wilms Jan 02 '22 at 19:35
  • 1
    @bergi hm, initially I thought the term semantics was used in a broader way here, though after reading some other parts like "The actual semantics of objects, in ECMAScript, are specified via algorithms called internal methods. Each object in an ECMAScript engine is associated with a set of internal methods that defines its runtime behaviour" it seems the terms "behaviour" and "semantics" are used to refer to the same thing, in which case I agree to your disagreement – Jonas Wilms Jan 02 '22 at 19:47
  • Yes, I'd say "semantics" (of a programming language) are the "rules of behaviour" (of entities within it). – Bergi Jan 02 '22 at 20:20