Asked  7 Months ago    Answers:  5   Viewed   49 times

What is the difference between this constructor-based syntax for creating an object:

person = new Object()

...and this literal syntax:

person = {
    property1 : "Hello"

It appears that both do the same thing, although JSLint prefers you use object literal notation.

Which one is better and why?



They both do the same thing (unless someone's done something unusual), other than that your second one creates an object and adds a property to it. But literal notation takes less space in the source code. It's clearly recognizable as to what is happening, so using new Object(), you are really just typing more and (in theory, if not optimized out by the JavaScript engine) doing an unnecessary function call.


person = new Object() /*You should put a semicolon here too.  
It's not required, but it is good practice.*/ 

person = {
    property1 : "Hello"

technically do not do the same thing. The first just creates an object. The second creates one and assigns a property. For the first one to be the same you then need a second step to create and assign the property.

The "something unusual" that someone could do would be to shadow or assign to the default Object global:

// Don't do this
Object = 23;

In that highly-unusual case, new Object will fail but {} will work.

In practice, there's never a reason to use new Object rather than {} (unless you've done that very unusual thing).

Tuesday, June 1, 2021
answered 7 Months ago

new {...} always creates an anonymous object, for instance:

  Object sample = new {};
  String sampleName = sample.GetType().Name; // <- something like "<>f__AnonymousType0" 
                                             //                    not "Object"

while new Object() creates an instance of Object class

  Object sample = new Object() {};
  String sampleName = sample.GetType().Name; // <- "Object"

since all objects (including anonymous ones) are derived from Object you can always type

  Object sample = new {};
Sunday, August 8, 2021
answered 4 Months ago

There's no difference. Parentheses are optional when using a function as a constructor (i.e. with the new operator) and no parameters. When not using the new operator, parentheses are always required when calling a function.

As noted in another answer, it's generally preferable to use an object literal instead. It has the following advantages over using the Object constructor:

  • Allows concise initialization of properties (e.g. var foo = { bar: "cheese" };)
  • Shorter
  • Possibly faster
  • Unaffected in the (unlikely) event of the Object function being overwritten.
Tuesday, August 24, 2021
John Oleynik
answered 4 Months ago

Try this:

 /*global arrayContainer:true, SliderInstance:true, DomObjects:true, document, Slider*/

Informs JSLint that these globals are assigned intentionally.

Tuesday, September 14, 2021
answered 3 Months ago

Check the assume a browser checkbox and on the Predefined section, type jQuery and $.

Friday, October 22, 2021
Christopher Francisco
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :