JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Object Object

Name

Object Object---features of all JavaScript objects

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Constructor

new Object();
new Object(value);    Navigator 3.0

Arguments

value

In Navigator 3.0, this optional argument specifies a primitive JavaScript value--a number, a Boolean, a string, or a function--that is to be converted to an object.

Returns

If no value argument is passed, this constructor returns a newly created object, which has no properties defined. If a primitive value argument is specified, then the constructor creates and returns a Number, Boolean, String, or Function object wrapper around the primitive value.

Properties

constructor

A read-only reference to the JavaScript function that was the constructor for the object. This property is not defined in Navigator 2.0 or Internet Explorer 3.0.

Methods

assign()

A method, which, if defined, is used to implement the JavaScript assignment operator (=).

eval()

Evaluate a string of JavaScript code in the context of the given object. In Navigator 2.0 and Internet Explorer 3.0, eval() is a standalone function, rather than a method of Object.

toString()

A method, which, if defined, is used to convert an object to a string.

valueOf()

Return the primitive value of the object, if any. For objects of type Object, this method simply returns the object itself. For other object types, such as Number and Boolean, this method returns the primitive value associated with the object. This method was added in Navigator 3.0.

Description

The Object object is a built-in data type of the JavaScript language. It serves as the "superclass" for all other JavaScript objects, and therefore methods of the Object object are methods of all other object types. Similarly, the behavior of the Object object is shared by all other object types. The basic behavior of objects in JavaScript is explained in Chapter 7, Objects.

A number of the Object methods listed above are unusual in that they are not predefined, and are not intended to be invoked by the JavaScript code that you write. Instead these are methods that you can define for any object, and that will be invoked by the JavaScript system at appropriate times, to perform some sort of operation on the object. The toString() method is an example: once you have defined this method for an object, the system will invoke it whenever it needs to convert that object to a string.

JavaScript allows object syntax to be used to refer to properties and methods of primitive datatypes, such as JavaScript strings. When you do this with JavaScript expressions such as "hello".toUpperCase(), what actually happens is that JavaScript creates a temporary object "wrapper" for the primitive value (a string in this case) so that the method can be invoked or the property accessed. This primitive value to object conversion is performed automatically by JavaScript whenever necessary, but if you ever need to perform it yourself, you can do so by passing the primitive value to the Object() constructor.

Usage

When an Object object is newly created, it has no properties defined. As described in Chapter 7, Objects, you can add a property definition to an object simply by assigning a value to the property. Furthermore, objects can be used as associative arrays. In some cases you will want to create an "empty" object of this sort for use in your programs. You do this with:

blank_obj = new Object();

In other cases, however, you will want to use a number of objects that are all of a certain type, such as objects that represent complex numbers, for example. To do this, you should write a constructor function for your object "class" and initialize the properties of the object as desired. You may also want to define a prototype object for your constructor function that will provide methods for all objects of your new object type.

Example

Defining the toString() method, and also the less frequently used assign() and valueOf() methods of an object, is most efficiently done in a constructor method for your object type, or with the prototype object of your object.

// define a constructor for the Complex object type
function Complex(x,y) { this.x = x; this.y = y; }
// Create a dummy object to force the prototype object to be created
new Complex(0,0);
// give all Complex objects a toString() method
Complex.prototype.toString = 
    new Function("return '{' + this.x + ',' + this.y + '}';");
// Create an object of this new Complex type
c = new Complex(2, 2);
// Convert the object to a string, implicitly invoking the
// toString() method, and display the string.
alert("c = " + c);   // displays string "{2,2}"

See Also

"Array", "Boolean", "Function", "Function.prototype", "Number", "String", "Window", Chapter 7, Objects


Previous Home Next
Number.valueOf() Book Index Object.assign()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.