What makes the availability of both primitive and object oriented values ​​in JavaScript useful?

I wrote a blog post some time ago detailing the availability of both primitive and object value types in JavaScript (for things like Number, String, and Boolean) causing problems, including, but not limited to, casting to logical (for example, wrapped with NaN objects, "" and false actual type conversion to true).

My question, with all this confusion and problems, is there any benefit to JavaScript having both types of values ​​for inline classes?

Edit: Thanks for the quick answers. I think JavaScript creators designed for wrapped natives, as a way to make scalar values, have child methods, but this is clearly the opposite, which causes more problems.

+4
source share
3 answers

JavaScript, like many languages, has good parts and bad parts .

This is one of the really very bad parts .

IMHO, in fact, not very much benefit, only harm from typed wrappers.

Our friend Douglas Crockford was in all this, in fact he was against him from the first day. That is all you need to know.

0
source

According to Douglas Crockford , they will never be useful:

Typed wrappers are completely unnecessary and occasionally confusing. Do not use new Boolean or new Number or new String .

Source: JavaScript: Good Details - Appendix B: Bad Parts (Page 114).

He even recommended his rejection for the 4th edition of ECMAScript .

+5
source

Douglas Crockford, although he is one of the smartest guys, is not God - everything he says should not be blind. There is actually one situation where you would like to prefer wrappers against primitive types - if you want to pass values ​​by reference.

Primitive values ​​are always passed by value and objects by reference . Therefore, if for some reason you need to pass numbers by reference , then you can do this using Number objects. In fact, you cannot change the value of a number without losing the link (AFAIK), but you can add additional parameters as desired, as with any object - primitive numbers do not support something.

 var nr1 = new Number(123), nr2 = nr1; // reference to nr1 nr1.name = "number"; //parameter "name" for nr1 is set AFTER the initialization of nr2 alert(nr2.name); // nr2 has the same parameter as nr1 
0
source

Source: https://habr.com/ru/post/1308590/


All Articles