tags:

views:

164

answers:

1

It's not the setup to a joke, i'm really asking.

Douglas Crockford is fond of saying that in the javascript prototypal object-oriented language there is no need for new.

He explains that new was simply added to give people coming from class-based (i.e. "classical") object oriented programming languages some level of comfort:

JavaScript, We Hardly new Ya

JavaScript is a prototypal language, but it has a new operator that tries to make it look sort of like a classical language. That tends to confuse programmers, leading to some problematic programming patterns.

You never need to use new Object() in JavaScript. Use the object literal {} instead.

Okay, fine:

  • new bad
  • {} good

But then commenter Vítor De Araújo pointed out that the two are not the same. He gives an example showing that a string is not like an object:

A string object and a string value are not the same thing:

js> p = "Foo"
Foo
js> p.weight = 42
42
js> p.weight // Returns undefined

js> q = new String("Foo")
Foo
js> q.weight = 42
42
js> q.weight
42

The string value cannot have new properties. The same thing is valid for other types.

What is going on here that an string is not an object? Am i confusing javascript with some other languages, where everything is an object?

+23  A: 

"Everything is an object"... that's one of the big misconceptions that exist all around the language.

Not everything is an object, there are what we call primitive values, which are string, number, boolean, null, and undefined.

That's true, a string is a primitive value, but you can access all the methods inherited from String.prototype as if it were an object.

The property accessor operators (the dot and the bracket notation), temporarily convert the string value to a String object, for being able to access those methods, e.g.:

"ab".charAt(1); // "b"

What happens behind the scenes is something like this:

new String("ab").charAt(1); // "b", temporal conversion ToObject

As with the other primitive values, such as Boolean, and Number, there are object wrappers, which are simply objects that contain the primitive value, as in your example:

var strObj = new String("");
strObj.prop = "foo";

typeof strObj; // "object"
typeof strObj.prop; // "string"

While with a primitive:

var strValue = "";
strValue.prop = "foo";

typeof strValue; // "string"
typeof strValue.prop; // "undefined"

And this happens because again, the property accessor on the second line above, creates a new temporal object, as:

var strValue = "";
new String(strValue).prop = "foo"; // a new object which is discarded
//...
CMS
I prefer to not use uppercase with primitives, like `boolean` and `number`, to avoid confusion with the object wrappers `Boolean` and `Number` (but the ECMAScript specification does not do so). Also, `NaN` is a primitive value, too.
Marcel Korpel
Marcel, yeah, object wrappers can cause confusion, e.g. `if (new Boolean(false)) {alert(':D');}`. Yes, `NaN`, positive and negative `Infinity` are values of the [Number type](http://ecma262-5.com/ELS5_HTML.htm#Section_8.5).
CMS
Ah, yes, `Infinity`, I already thought I forgot some other value. How can one ever forget `Infinity`?
Marcel Korpel
I want to stress that when using object methods to primitive values, the variable is only *temporarily* converted to an object and after the proposed operation, the object is converted back to a primitive. This concept can cause confusion, as you can see in [String object versus literal - modifying the prototype?](http://stackoverflow.com/q/3756549/258127)
Marcel Korpel