O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.1007/978-1-4302-6107-0_9

9. Working with Objects

Russ Ferguson and Keith Cirkel2
(1)
Ocean, New Jersey, USA
(2)
London, UK
 

What Is the Difference Between an Object Constructor and an Object Literal ?

Problem

You want to know what the difference is between creating an object using the new operator or an object literal.

Solution

Object literals are usually easer to read. When creating an object literal, you can assign properties at the same time. Using a object constructor can create an object but will require extra lines to define properties.

The Code

var R2D2 = new Object();
    R2D2.class = 'Astromech Droid';
var R2D2 = {
         class:'Astromech Droid';
         manufacturer: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required