From: johncoltrane on
Hi, a few months ago I've played with the JSON Tumblr API. I tried
(succesfully) to build a page dynamically and then went to other
experiments.

Now that I came back to playing with said API I'm intrigued by this
piece of code I wrote :

var newPost = function()
{
this.element = new Object(document.createElement('div'));
// other stuff
};

Well, what is intriguing here is that I never use new Object() in such
a context and always go with something like :

this.element = document.createElement('div');

Now I'm not asking for a psycho-analysis; I would just want to know if
there is any actual difference between the two ways.

Thanks

---
JohnColtrane
From: Dmitry A. Soshnikov on
On 20.05.2010 11:43, johncoltrane wrote:

<snip>

>
> var newPost = function()
> {
> this.element = new Object(document.createElement('div'));
> // other stuff
> };
>
> Well, what is intriguing here is that I never use new Object() in such
> a context and always go with something like :
>
> this.element = document.createElement('div');
>
> Now I'm not asking for a psycho-analysis; I would just want to know if
> there is any actual difference between the two ways.
>

To answer this question you should look on:

15.2.2.1 new Object ( [ value ] ) of ES3:

....
3. If the value is a native ECMAScript object, do not create a new
object but simply return value.
4. If the value is a host object, then actions are taken and a result is
returned in an implementationdependent
manner that may depend on the host object.
....

And to:

15.2.2.1 new Object ( [ value ] ) of ES5:

1. If value is supplied, then
a. If Type(value) is Object, then
i. If the value is a native ECMAScript object, do not create a
new object but simply return value.
ii. If the value is a host object, then actions are taken and a
result is returned in an implementation-dependent manner that may depend
on the host object.

So, you pass a host object. That means, the behavior is an
implementation dependent.

Applying native objects for this operation makes no a big sense, because
the argument just will be returned without modifications:

var a = [];
a === new Object(a); // true

Dmitry.