CHAPTER 9

image

Objects

An object is a collection of named values known as properties. Properties can be either variables that hold the state of the object, or functions that define what the object can do. The appeal of objects is that they provide functionality while hiding their inner workings. All you need to know is what an object can do for you, not how it does it.

Object Properties

An empty object can be created using the new directive in the following explicit manner.

var box = new Object();

Properties for an object are created automatically when they are assigned to, using either the dot notation or the array notation.

box.x = 2;box["y"] = 3; ...

Get JavaScript Quick Syntax Reference 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.