The statement "avoid creating objects" in itself is absurd in JavaScript, which has objects everywhere and is one of the most object-oriented languages. But "avoid creating object versions of primitives," which is the code you specify, really. That is, avoid new String , new Number and new Boolean .
JavaScript has both primitive and object versions of strings, numbers, and Booleans. There was almost never a reason to create an object version of any of them explicitly, and this can lead to confusion; see inline comments:
Object versions of strings, numbers, and booleans basically exist to allow primitives to be provided to methods using the same mechanism that provides methods for object types. When you do
console.log("foo".toUpperCase()); // "FOO"
a temporary object is created for the primitive string "foo" , and then the toUpperCase property is read from this object. Since the object inherits from String.prototype , it has toUpperCase , and all is well. When the operation is completed, the temporary object is discarded (unless something refers to it, but does nothing and cannot do anything with toUpperCase , you need to add the String.prototype method, which returns the object in order to be held).
TJ Crowder Jan 04 '17 at 7:16 2017-01-04 07:16
source share