While I was learning the language, I became interested in primitive types and their object wrappers.
While I was in my code editor discovering this language feature, I discovered that any of these object wrappers could be instantiated with the keyword “new.” I then discovered that not using it (new) transforms this “constructor” into a conversion function. Very well, the documentation tells me that this is an intentional feature of the language.
However, as I continued testing, I realized that trying to instantiate the BigInt and Symbol wrapper objects was impossible (returns a TypeError).
So, I went to read the documentation, and it clearly states that:
- Attempting to construct it with new throws a TypeError.
- Performs a type conversion when called as a function rather than as a constructor (like all other object wrappers).
- Is not intended to be used with the new operator or to be subclassed.
But nothing more!
The question I’m asking myself is: Why can all other wrapper objects be instantiated with the keyword new except for BigInt and Symbol?
My hypothesis was that in the symbol example, it is a bit of a special case in the sense that it is not “intended” to be manipulated, hence the instantiation.
Sorry for this long message, I feel like the answer is obvious and, worse, right in front of my eyes.
PS: I know that I will never instantiate wrapper objects in JavaScript in a real-world situation, I’m just wondering because I’m curious about what happens under the hood.
By the way, if you have any anecdotes or websites that explain in detail how things work, I’d love to hear them.