9

In JavaScript, what's the difference between

var x = Object();

and

var x = new Object();

?

1
  • @Babiker: Yep, that's the question I was looking for, but it turns out it's not similar at all. Commented Jun 20, 2010 at 19:39

1 Answer 1

9

This is pulled directly from the ECMAScript specification:

15.2.1 The Object Constructor Called as a Function

When Object is called as a function rather than as a constructor, it performs a type conversion.

15.2.1.1 Object ( [ value ] )

When the Object function is called with no arguments or with one argument value, the following steps are taken:

  1. If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments (15.2.2.1).

    In Short: new Object([ value ])

  2. Return ToObject(value).

Notes:

[ ] Is A common way to mark a parameter as optional.

ToObject Is a very simple operation that is defined in section 9.9.

Sign up to request clarification or add additional context in comments.

5 Comments

So it called as a function with no arguments what does it do? Convert undefined to an object?
It simply returns the equivalent of calling new Object().
@Skilldrick: It would "create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments." So it would be the same as simply saying new Object().
So there is no difference between Object() and new Object()?
@Jens - The final result is the same. The only difference being a layer of redirection to the internal method Construct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.