Object ObjectNameObject Object---features of all JavaScript objectsAvailabilityNavigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0 Constructor
new Object(); new Object(value); Navigator 3.0 Arguments
ReturnsIf 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
Methods
DescriptionThe 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. UsageWhen 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. ExampleDefining 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}" |
|