The JavaScript Object Object


The JavaScript Object Object

Here's another way to create custom objects, as of Netscape Navigator 3 and Internet Explorer 4: You can base them on the JavaScript Object object explicitly. In fact, all JavaScript objects are based on the Object object now, and that means any object you create with the new operator.

For example, here's how I create a new object, object1 , and add a property to it, value , and then store a number in that property and display the stored value:

(Listing 23-10.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Creating a Custom Object          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function creator()              {  var object1 = new Object()   object1.value = 5   document.form1.text1.value = "object1.value = " + object1.value  }              // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Creating a Custom Object</H1>          <FORM NAME="form1">              <INPUT TYPE="BUTTON" ONCLICK="creator()" VALUE="Click Me!">              <BR>              <INPUT TYPE="TEXT" NAME="text1">          </FORM>      </BODY>  </HTML> 

You can see the results in Figure 23.11, where we've created a new object using the JavaScript Object object, added a property to it, stored a value in that property, and displayed the stored value.

Figure 23.11. Creating a new object using the JavaScript Object object.

graphics/23fig11.gif

You can find the properties and methods of the Object object in Table 23.1 in overview, its properties in depth in Table 23.2, and its methods in Table 23.3 in depth.

Table 23.1. Overview of the Properties and Methods of the Object Object

Properties

Methods

constructor

hasOwnProperty

propertyIsEnumerable

isPrototypeOf

prototype

toLocaleString

 

toString

 

valueOf

Table 23.2. The Properties of the Object Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

constructor

 

x

x

x

   

x

x

x

x

 

Read/write

 

This property specifies the function that creates an object.

propertyIsEnumerable

     

x

       

x

x

 

Read/write

 

You use this property like this: object .propertyIsEnumerable ( property ) . (It looks more like a method, but Microsoft calls it a property.) This property is true if property exists in object and can be looped over using a for...in loop, and false otherwise .

prototype

 

x

x

x

   

x

x

x

x

 

Read/write

 

This property returns a reference to the object's prototype. You use the prototype property to provide base functionality to a set of objects. See "Adding Methods to Existing Objects" in this chapter for more information.

Table 23.3. The Methods of the Object Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

hasOwnProperty

     

x

       

x

x

 

Returns: Boolean

 

This method returns true if an object has a property of the specified name, false if it does not. Syntax: object.hasOwnProperty( name ) , where name is the name of the property you're checking.

isPrototypeOf

     

x

       

x

x

 

Returns: Boolean

 

This method returns true if an object has the current object in its prototype set.

 

Syntax: object.isPrototypeOf( name ) , where name is the name of the object you're checking.

toLocaleString

     

x

       

x

x

 

Returns: String

 

Returns an object's data converted to a string using the current locale.

 

Syntax: object .toLocaleString() .

toString

     

x

       

x

x

 

Returns: String

 

Returns an object's data converted to a string.

 

Syntax: object .toString() .

valueOf

     

x

       

x

x

 

Returns: Object

 

Returns an object's data.

 

Syntax: object .valueOf() .



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net