Object-Oriented Programming with JavaScript, Part I: Inheritance: Classifying and Printing Objects - Doc JavaScript | WebReference

Object-Oriented Programming with JavaScript, Part I: Inheritance: Classifying and Printing Objects - Doc JavaScript

Object-Oriented Programming with JavaScript, Part I: Inheritance

Classifying and Printing Objects

JavaScript supports three types of objects: native, host, and user-defined. Native objects are objects supplied by the JavaScript language. Object, Math, and Number are examples of native objects. Native object names start with a capital letter, and they are case-sensitive. To find the value of the mathematical constant PI, type Math.PI. If you try math.PI, you'll get an error message. Host objects are provided by the browser for the purpose of interaction with the loaded document. Examples for host objects are: document, window, and frames. Host object names start with a lower-case letter. You can print on the screen the value of PI by document.write(Math.PI). Try Document.write() and you'll get an error message. You define user-defined objects. You name the objects and you implement them. Your objects can start with either a lower-case letter or an upper-case letter, but they are case-sensitive. Here is an example that gives an error message because of case mismatch (try it):

function employee() {
  this.dept = "HR";
  this.manager = "John Johnson";
function printProp() {
  var ken = new Employee();
  for (property in ken) {

The Object object is the base object for all native and user-defined objects. All objects inherit from this superclass. You can create an Object object as well:

var myObject = new Object();

The Object object has a dozen of properties and methods. Examples are constructor, toString(), and valueOf(). The object myObject above sports these properties. Alert the object. You should get [object Object]. You can pass a string to the Object constructor:

var myObject = new Object("foo");

Alert the object. You should get the string you passed to the constructor, "foo". The native objects sports the same properties, because they inherit from the Object class. Probe the constructor property of the Math object, Math.constructor. The answer you should get is that the Object object is the constructor of the Math object.

When you want to get a uniform feedback about an object's names and content, the best way is to ask for their string version. Use the object's methods of toString(), toLocaleString(), and valueOf(). When the object consists of a number, like:

var myNum = new Number(35);

the toString(), toLocaleString(), and valueOf() methods return the string "35". When the object is user-defined, they return [object Object]. Try it for the following Employee object:

function Employee() {
  this.dept = "HR";
  this.manager = "John Johnson";
function printProp3() {
  var Ken = new Employee();

The toLocaleString() prints its object according to your definitions in the Control Panel's Regional Settings file.

Next: How to query an object's properties


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: March 12, 2001
Revised: March 12, 2001

URL: http://www.webreference.com/js/column79/8.html