Object object
Represents the base object for all JavaScript objects.
- The Object object inherits from the Function.prototype object and the Function.prototype object inherits from the Object.prototype object.
- The Object.prototype object is the base object for all JavaScript objects.
- The Object object inherits all of its properties and methods from the Function.prototype and Object.prototype objects, except the prototype property that refers to the Object.prototype object.
An arbitrary object can be created with the constructor of the Object object. When the constructor of the Object object is called without parameters or with null or undefined, the type of the created object is Object. If the constructor is called with a primitive data type, then the type of the created object is: Boolean in case of primitive boolean, Number in case of a primitive number, String in case of a primitive string. If the constructor is called with an object, then the type of the created object is the corresponding object.
Syntax:
Creating a new Object instance:
var obj = new Object ([value]);
The value parameter is optional an can be of any type.
Use the valueOf method to retrieve the value of an object.
Another way to create an Object instance:
var obj = { member1 : value1, member2 : value2, ..., memberN : valueN };
where member1, member2, ..., memberN are the initial members of the newly created object with the values of value1, value2, ..., valueN.
Member names that contain spaces or other special characters (a JavaScript identifier can only contain alphanumeric characters, underscores (_) and dollar signs ($) and the first character cannot be a digit), must be quoted.
See the Examples section below, it contains several examples for object creation.
Member names that contain spaces or other special characters (a JavaScript identifier can only contain alphanumeric characters, underscores (_) and dollar signs ($) and the first character cannot be a digit), must be quoted.
See the Examples section below, it contains several examples for object creation.
Members:
Properties:
Property | Support | Description | |||||
---|---|---|---|---|---|---|---|
constructor* | Returns a reference to the constructor function that created the current object. | ||||||
prototype | Returns a reference to the Object.prototype object. The Object.prototype object allows adding properties and methods to the Object object that can be used with instances of the Object object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Object object, only Object.prototype is allowed. |
(*) - The property is inherited from the Object.prototype.
Methods:
Method | Support | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
__defineGetter__(propertyName, function)* |
Creates a getter method for the specified property.
The getter method is called whenever the property value is retrieved.
This method is deprecated, use the cross-browser get operator instead.
|
||||||||||
__defineSetter__(propertyName, function)* |
Creates a setter method for the specified property.
The setter method is called whenever a value should be assigned to the property.
This method is deprecated, use the cross-browser set operator instead.
|
||||||||||
hasOwnProperty (propertyName)* |
Returns a boolean value that indicates whether the current object has a predefined property with the given name, or not.
|
||||||||||
isPrototypeOf (objectToTest)* |
Returns a boolean value that indicates whether the current object is in the prototype chain of the specified object.
|
||||||||||
propertyIsEnumerable (propertyName)* |
Returns a boolean value that indicates whether the given property can be enumerated by a for...in loop in the current object.
|
||||||||||
toLocaleString ( )* | Returns a localized string representing the value of the object | ||||||||||
toSource ( )* | Returns a string representing the source code of the object. | ||||||||||
toString ( )* | Returns a string representing the value of the object. When an object needs to be converted to a string, the JavaScript interpreter automatically calls its toString method. | ||||||||||
unwatch (propertyName)* |
Removes the function that has been attached to the specified property with the watch method.
|
||||||||||
valueOf ( )* | Returns a primitive value from the current object. | ||||||||||
watch (propertyName, function)* |
Attaches a function that is invoked every time when the value of the specified property is changed.
|
(*) - The method is inherited from the Object.prototype.
Examples:
Example 1:
Creating a new object and declaring a member function.
Example 2:
Another solution for the previous example.
Example 3:
This example creates an object (point) with two properties (x and y).
Example 4:
This example is the same as the previous one, but it declares members with quoted names.
Example 5:
This example is the conclusion of the previous ones.
It creates identical Object instances in several different ways.
Example 6:
Checking predefined properties.
Example 7:
This example shows how the isPrototypeOf method and the instanceof operator work.
Example 8:
This example illustrates the use of the watch and unwatch methods in Firefox.
Example 9:
This example shows how to add getter and setter methods to the Date object.
The attached methods can be used in all instances of the Date object.
|
External links:
User Contributed Comments