A question about new Object()

J

johncoltrane

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
 
D

Dmitry A. Soshnikov

On 20.05.2010 11:43, johncoltrane 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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top