Skip to Content
JavaScript: The Definitive Guide, Fourth Edition
book

JavaScript: The Definitive Guide, Fourth Edition

by David Flanagan
November 2001
Intermediate to advanced
936 pages
68h 43m
English
O'Reilly Media, Inc.
Content preview from JavaScript: The Definitive Guide, Fourth Edition

Object Properties and Methods

As we discussed earlier, all objects in JavaScript inherit from the Object class. While more specialized classes, such as the built-in String class or a user-defined Complex class, define properties and methods of their own, all objects, whatever their class, also support the properties and methods defined by the Object class. Because of their universality, these properties and methods are of particular interest.

The constructor Property

Starting with JavaScript 1.1, every object has a constructor property that refers to the constructor function used to initialize the object. For example, if I create an object o with the Complex( ) constructor, the property o.constructor refers to Complex:

var o = new Complex(1,2);
o.constructor == Complex;  // Evaluates to true

Each Complex object (or object of whatever type) does not have its own unique constructor property, of course; instead, this property is inherited from the prototype object. As discussed earlier in this chapter, JavaScript creates a prototype object for each constructor function you define and assigns that object to the prototype property of the constructor. What I did not reveal earlier, however, is that the prototype object is not initially empty. When created, it includes a constructor property that refers to the constructor function. That is, for any function f, f.prototype.constructor is always equal to f (unless we set it to something else).

Since the constructor function defines the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

John Pollock
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 0596000480Supplemental ContentCatalog PageErrata