18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Basics<br />

ECMAScript objects are one of the unique (and useful) features of <strong>JavaScript</strong>. Chapter 2,<br />

“ECMAScript Basics,” introduced the concept that everything is an object, including functions.<br />

This chapter focuses on how to manipulate and use those objects, as well as how to create your<br />

own objects to add functionality specific to your needs.<br />

Object-Oriented Terminology<br />

ECMA-262 defines an object as an “unordered collection of properties each of which contains a<br />

primitive value, object, or function.” Strictly speaking, this means that an object is an array of values<br />

in no particular order. Although this is ECMAScript’s interpretation, an object is more generically<br />

defined to be a code-based representation of a noun (person, place, or thing).<br />

Each object is defined by a class, which can be thought of as a recipe for an object. The class defines<br />

both the interface of an object (the properties and methods that can be accessed by developers) as<br />

well as the inner workings of the object (the code that makes the properties and methods work).<br />

The compiler or interpreter uses the class to build objects according to its specifications.<br />

When a program uses a class to create an object, the resulting object is said to be an instance of the<br />

class. The only limit to the number of instances that can be created from a single class is the physical<br />

memory limitations of the machine on which the code is running. Each instance behaves the<br />

same way, but each can handle separate sets of data. The process of creating an object instance<br />

from a class is called instantiation.<br />

As I discussed briefly in Chapter 1, ECMAScript has no formal classes. Instead, ECMA-262 describes<br />

object definitions as the recipes for an object. This is a logical compromise for ECMAScript, because<br />

object definitions actually are objects in and of themselves (which I explain shortly). Even though<br />

classes don’t actually exist, this book refers to object definitions as classes because the term is more<br />

familiar to most developers and, functionally, the two are equivalent.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!