Ben Aston
Ben Aston

Reputation: 55739

Primitives vs structural types in JavaScript

MDN discriminates between primitives like Number, Boolean and String, and structural types like Object, although the term "structural type" is not used in the specification.

What does MDN mean by "structural types" in this context? Are all non-primitives in a language structural? Is it referring to a structured type system?

Upvotes: 2

Views: 224

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138277

I think the reason for introducing a new unspecified term here is because the underlying abstraction the language provides is not particularly useful when writing code.

In the specification, both Objects and all the others (Numbers, Booleans, ...) are just Values, there is no difference between them. However with the abstract operations on Objects, you can mutate them, the other Values do not have that behaviour. Therefore one could say that all values are "passed by reference", Functions are Objects with a [[Call]] internal property, and so on. That would be technically correct and it would use the terms used in the specification.

However that's not really useful. It is way easier to explain the languages behaviour with "primitive values" (all the ones that cannot be mutated, so all non Objects). It is also way easier to treat functions and objects as different things (instead of saying that Functions are exotic Objects).

Upvotes: 2

Related Questions