最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why does JavaScript have boxed versions of primitives? - Stack Overflow

programmeradmin1浏览0评论

Why does JavaScript have boxed versions of primitives?

Like, I'm good with true and false, why do I need

new Boolean(true) === new Boolean(true) // false

I get that languages like Java and Objective-C had the boxed things because you couldn't put primitives into the same data structures as non-primitives. But JS always allowed arrays of mixed things [1, 2, {}, ""] and anything could be the value for a key in an object.

I'm not asking for a justification: of course capital "B" Boolean is nonsense. But I'm genuinely curious about how this happened. There must have been a reason at the time.

Why does JavaScript have boxed versions of primitives?

Like, I'm good with true and false, why do I need

new Boolean(true) === new Boolean(true) // false

I get that languages like Java and Objective-C had the boxed things because you couldn't put primitives into the same data structures as non-primitives. But JS always allowed arrays of mixed things [1, 2, {}, ""] and anything could be the value for a key in an object.

I'm not asking for a justification: of course capital "B" Boolean is nonsense. But I'm genuinely curious about how this happened. There must have been a reason at the time.

Share Improve this question asked Mar 31, 2020 at 21:29 Max HeiberMax Heiber 15.6k13 gold badges73 silver badges109 bronze badges 2
  • 1 Does this answer your question? What is the purpose of new Boolean() in Javascript? – Hamms Commented Mar 31, 2020 at 21:32
  • @Hamms I don't think it does – Bergi Commented Mar 31, 2020 at 21:57
Add a ment  | 

1 Answer 1

Reset to default 9

JavaScript has boxed primitive types so that their instances can inherit properties (especially methods) from their prototypes, which fits in well with the auto-boxing of the receiver value on property access (and method calls).

For example

Boolean.prototype.demo = "!";
console.log(true.demo);
console.log(true.toString());

Contrast this to undefined.toString(), which is not auto-boxed.

This reason is confirmed by the creator of the language:

the so-called primitive types Boolean, Number, and String each have a corresponding Object subtype: Boolean, Number, and String respectively. When a primitive value is used as an object, it is automatically “boxed” or wrapped by a new instance of the corresponding object subtype. When used in an appropriate primitive type context, the box/wrapper converts back to the primitive value.

However, he actually was rather unhappy with that, and wanted to change the type system for ES4 (which, we all known, never happened).

The question could actually even have been framed the other way round: "Why does JavaScript have primitive types when it already has Boolean, Number and String objects?". They really aren't necessary at all, many languages do fine without them and just live the object-oriented everything is an object maxim - including languages that were the inspirations for JS.

Again, we learn from Brendan Eich:

The diktat from upper engineering management was that the language must “look like Java”. […]

I’m not proud, but I’m happy that I chose Scheme-ish first-class functions and Self-ish (albeit singular) prototypes as the main ingredients. The Java influences, especially y2k Date bugs but also the primitive vs. object distinction (e.g., string vs. String), were unfortunate.

发布评论

评论列表(0)

  1. 暂无评论