4 ms·
> Note that there was no new. The new operator even throws a TypeError when we try it on Symbol. Why? That seems inconsistent with other JavaScript constructor
by nailer 11y ago
> Note that there was no new. The new operator even throws a TypeError when we try it on Symbol.
Why? That seems inconsistent with other JavaScript constructors.
- thomasfoster96 11y agoIt's because the Symbol() function is creating a primitive value, not an object. There is no literal syntax for symbols, as there is for numbers, strings, etc., and so the symbol function is how you make them. Having a symbol constructor would be of little use (it'd be like using new Number()) and so it wasn't included.
- kevincennis 11y agoJust to expand on that a bit for anyone who might not know this: var s = new String('foo'); typeof s; // 'object' That is, when you use the `new` keyword, you get back an object instead of a primitive. Since symbols are always primitives, disallowing the `new` keyword actually prevents inconsistency with the rest of the language in the sense that it doesn't allow you to create non-objects with a constructor.
- draw_down 11y agoStrings are always primitives too, but you can call `new String` and receive an object, not an error. This is just straight-up inconsistent, I don't see the point of arguing otherwise.
- thomasfoster96 11y agoTake a look at this[0] with regards to strings. I'm not really sure why you'd use new String anyway - it won't always act as a string primitive. Rather than making Symbol reimplement the same quirks that other built in constructors do, isn't not allowing a Symbol constructor going to avoid a lot of confusion? For example, if you created an object using a hypothetical symbol constructor, the symbol would be converted to a string if you tried to use it as an object key (internally, JavaScript sees your symbol object as an object, not a symbol), which is simply creating problems for a naive programmer. [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FString#Distinction_between_string_primitives_and_String_objects https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- draw_down 11y agoThe point under debate is whether it's inconsistent with Number, String, and Boolean, which it is. I agree `new String` is not particularly useful.
- thomasfoster96 11y agoSorry, I think I misunderstood your point. I thought you meant that inconsistency was worse than misusing constructors.
- nailer 11y agoOk so it's unique in that unlike Number, String, Boolean it has an primitive version but no object version. That could still make it not error when called the same was as other constructors.
- thomasfoster96 11y ago> That could still make it not error when called the same was as other constructors. What do you mean? That new Symbol() should just ignore the 'new'?
- nailer 11y ago'Was' = 'way'. Typo, sorry.
- AgentME 11y agoIt's consistent with the other primitive functions: console.log(Boolean(5)); // true console.log(Number("5")); // 5 console.log(String(1)+1); // "11" Using new with any of the other primitive functions takes the return value and creates it into an object with the primitive function's prototype, which is kind of a coincidence it looks like it does anything useful but can cause a lot of issues because primitives and objects are different. If backwards compatibility weren't king, I'd expect to see that get removed. Instead, the best they can do is make any new primitive functions throw a TypeError if used with `new`.
- draw_down 11y agoIt's not consistent, calling those functions with `new` returns an object representation of the primitive value (you can get the primitive value by calling .valueOf or by coercing). It does not cause an error.
- AgentME 11y agoThe correct way of using it is consistent. I'd be surprised if being able to use `new` with the other primitive functions was ever specifically intended and if that choice to allow it wasn't regretted. It's a common source of confusion and is not useful.