© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.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

Listing 9-1. Objects Can Be Created Using Either the Object Constructor or by Creating an Object Literal
var R2D2 = ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.