JavaScript Object Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for this Object Class



The Object Class Reference Table
Native class
Super Class
Constants Properties Methods Constructor Function
Syntax Arguments Returns
 
None constructor hasOwnProperty( )
isPrototypeOf( )
propertyIsEnumerable( )
toLocaleString( )
toString( )
valueOf( )
new Object( )
new Object(value)
value A new object.




Object Class Description

The Object class acts as a super class for all other native classes. The Object class contains properties and methods that will be inherited by all other native classes. Whereas the Object class acts as the super class, the other native classes, that inherit from the Object class, all act as subclasses to the super class.





Object Class Constants

There are no explicit constants for the Object class.





Object Class instance Properties





Methods of the Super Object Class (Inherited by other Native Classes)

The Object class contains the methods as shown here. These methods are normally overridden by the like-named methods of other native subclasses. When not overridden by a subclass method, the Object methods become the default method.



The Methods for the Object class
Method / Syntax / Argument Returns Description / Example
 
Method:
hasOwnProperty( )

Syntax:
object.hasOwnProperty(name)

Arguments:
name: a string containing a property name
Returns true or false The hasOwnProperty( ) method behaves as follows:
  • the argument for this method should contain a string that is a name of a property for the object
  • returns false if the object does not have the property specified by the name
  • returns true if the object does have the property specified by the name
  • returns false if the object inherits the property from the prototype object
Example:
// create an empty object var obj = new Object( ); obj.shape = "circle";//assign a property obj.hasOwnProperty("shape");//return true; obj.hasOwnProperty("pi");//return false; obj.hasOwnProperty("valueOf");//return false;
Method:
isPrototypeOf( )

Syntax:
object.isPrototypeOf(name)

Arguments:
name: object name
Returns true or false The isPrototypeOf( ) method behaves as follows:
  • the argument for this method should contain a name of an object
  • returns false if the object is not the prototype of the object named in the argument
  • returns true if the object is the prototype of the object named in the argument
  • returns false if the argument is not an object
Example:
// create an empty object var obj = new Object( ); Object.prototype.isPrototypeOf(obj); // returns true
Method:
propertyIsEnumerable( )

Syntax:
object.propertyIsEnumerable(name)

Arguments:
name: property name
Returns true or false The propertyIsEnumerable( ) method behaves as follows:
  • the argument for this method should contain a name of a property of an object
  • returns false if the property specified as the argument is not an enumerable property of the object
  • returns true if the property specified as the argument is an enumerable property of the object
Example:
// create an empty object var obj = new Object( ); obj.color = "white"; obj.propertyIsEnumerable(color); // returns true
Method:
toLocaleString( )

Syntax:
object.toLocaleString( )

Arguments: none
Returns a string. The toLocaleString( ) method behaves as follows:
  • returns a string version of the object
  • the default toLocaleString( ) provided by the Object class simply calls the toString( ) method
  • see the toString( ) method for further details
Method:
toString( )

Syntax:
object.toString( )

Arguments: none
Returns a string. The toString( ) method behaves as follows:
  • returns a string version of the object
  • this method is overridden by toString methods of most subclasses
  • if not overridden, the Object.toString() method becomes the default
  • a call to Object.toString() whether as the default or an explicit call will return: [object class]
  • where "class" is the class of the object
  • to force an explicit call on the default toString method (see alert statement parm below):
//create a boolean wrapper object var bo = new Object(true); alert(Object.prototype.toString.apply(bo)); // returns [object Boolean]
  • when objects are used in a string context, JavaScript will implicitly invoke the toString method to convert the object to a string
  • To summarize:
    1. Since the Object class is rarely used, the Object.toString() method is seldom used.
    2. Subclasses of the Object class have there own toString methods that will override the super classes toString method.
    3. JavaScript will implicitly invoke the toString method when objects are used in a string context therefore, making the explicit invoking of the toString method unnecessary.
    4. User defined classes should always contain a toString method. Your own toString method will return a meaningful string and prevent the inheritance of the default toString method of the Object class.

Example:
// ddd is an empty object var ddd = new Object(); alert(ddd.toString()); // alert returns [object Object]
Method:
valueOf( )

Syntax:
object.valueOf( )

Arguments: None
Returns the primitive value The valueOf( ) method behaves as follows:
  • this method is normally overridden by valueOf methods of most other subclasses
  • JavaScript will automatically invoke the valueOf method whenever an object is used in a context where a primitive value is expected.
  • custom classes should contain a valueOf method if you do not want the automatic invocation of the default valueOf method of the Object class
  • valueOf( ) returns the primitive value contained in the object
  • if the object is not of a primitive type, the method returns the object itself
  • the Object.valueOf( ) method is suited for wrapper classes; the Number object would return the primitive number and the Boolean object would return the primitive boolean value
  • the Object.valueOf( ) method returns the string value for String objects
  • the Object constructor performs the wrapping and the Object.valueOf method performs the unwrapping.
Example:
// create a Number wrapper object var num = new Object(100); alert(num.valueOf())//evaluates to 100;




Object Class Constructor



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson