You are here: Reference > JavaScript > core > objects > Object

Object object

Browser support:
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.
Objects that can be instantiated inherit from the Function.prototype object in JavaScript, the others inherit from 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.

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.
propertyName Required. The name of the property whose value the function returns.
function Required. Reference to the getter function.
__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.
propertyName Required. String that specifies the name of the property.
function Required. Reference to the setter function.
hasOwnProperty (propertyName)*
Returns a boolean value that indicates whether the current object has a predefined property with the given name, or not.
propertyName Required. String that specifies the name of the property.
isPrototypeOf (objectToTest)*
Returns a boolean value that indicates whether the current object is in the prototype chain of the specified object.
objectToTest Required. The object whose prototype chain is to be checked.
See Example 7 for details.
propertyIsEnumerable (propertyName)*
Returns a boolean value that indicates whether the given property can be enumerated by a for...in loop in the current object.
propertyName Required. String that specifies the name of the property.
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.
propertyName Required. String that specifies the name of the property.
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.
propertyName Required. String that specifies the name of the property.
function Required. Specifies the function to call when the value of the property has been changed. The registered function gets the name, the previous and the new value of the modified property as parameters in this order and must return a value. The returned value will be the value of the property. See the Example 8 for details.

(*) - The method is inherited from the Object.prototype.

Examples:

Example 1:

Creating a new object and declaring a member function.
var myObj = new Object ();
myObj.myFunc = function () {
    alert ("My first member function!");
};
myObj.myFunc ();
Did you find this example helpful? yes no

Example 2:

Another solution for the previous example.
var myObj = {myFunc : function () {alert ("My first member function!")}};
myObj.myFunc ();
Did you find this example helpful? yes no

Example 3:

This example creates an object (point) with two properties (x and y).
var point = {x : 100, y : 50};
alert (point.x);    // or point["x"]
alert (point.y);    // or point["y"]
Did you find this example helpful? yes no

Example 4:

This example is the same as the previous one, but it declares members with quoted names.
var point = {"x" : 100, "y" : 50};
alert (point.x);    // or point["x"]
alert (point.y);    // or point["y"]
Did you find this example helpful? yes no

Example 5:

This example is the conclusion of the previous ones. It creates identical Object instances in several different ways.
var point1 = new Object ();
point1.x = 100;
point1.y = 50;
point1.scale = function (h, v) {this.x *= h; this.y *= v};
point1["x+y"] = function () {return this.x + this.y};

var point2 = new Object ();
point2["x"] = 100;
point2["y"] = 50;
point2["scale"] = function (h, v) {this.x *= h; this.y *= v};
point2["x+y"] = function () {return this.x + this.y};

var point3 = new Object ();
point3.x = 100;
point3.y = 50;
point3.scale = function (h, v) {this["x"] *= h; this["y"] *= v};
point3["x+y"] = function () {return this["x"] + this["y"]};

var point4 = {};
point4.x = 100;
point4.y = 50;
point4.scale = function (h, v) {this.x *= h; this.y *= v};
point4["x+y"] = function () {return this.x + this.y};

var point5 = {
    x : 100, y : 50, 
    scale : function (h, v) {this.x *= h; this.y *= v},
    "x+y" : function () {return this.x + this.y}
};

var point6 = {
    "x" : 100, "y" : 50, 
    "scale" : function (h, v) {this.x *= h; this.y *= v},
    "x+y" : function () {return this.x + this.y}
};

point1.scale (2, 3);    // or point1["scale"] (2, 3)
alert (point1["x+y"] ());

    // or for example
point5.scale (2, 3);    // or point5["scale"] (2, 3)
alert (point5["x+y"] ());
Did you find this example helpful? yes no

Example 6:

Checking predefined properties.
var arr = new Array ();
document.write (arr.hasOwnProperty ("length")); // display: true
document.write ("<br />");
document.write (arr.propertyIsEnumerable ("length")); // display: false
Did you find this example helpful? yes no

Example 7:

This example shows how the isPrototypeOf method and the instanceof operator work.
question = " object in the prototype chain of the ";
answer = Object.prototype.isPrototypeOf (Boolean);
document.write ("Is the Object.prototype" + question + "Boolean object? " + answer);
document.write ("<br />");
answer = Function.prototype.isPrototypeOf (Boolean);
document.write ("Is the Function.prototype" + question + "Boolean object? " + answer);
document.write ("<br />");

answer = Object.prototype.isPrototypeOf (Math);
document.write ("Is the Object.prototype" + question + "Math object? " + answer);
document.write ("<br />");
answer = Function.prototype.isPrototypeOf (Math);
document.write ("Is the Function.prototype" + question + "Math object? " + answer);
document.write ("<br />");

question = " object an instance of the ";
answer = Boolean instanceof Object;
document.write ("Is the Boolean" + question + "Object object? " + answer);
document.write ("<br />");
answer = Boolean instanceof Function;
document.write ("Is the Boolean" + question + "Function object? " + answer);
document.write ("<br />");

answer = Math instanceof Object;
document.write ("Is the Math" + question + "Object object? " + answer);
document.write ("<br />");
answer = Math instanceof Function;
document.write ("Is the Math" + question + "Function object? " + answer);
document.write ("<br />");
Did you find this example helpful? yes no

Example 8:

This example illustrates the use of the watch and unwatch methods in Firefox.
function OnPropertyChange (propName, oldValue, newValue) {
    alert ("The value of the " + propName + " property is modified from " + oldValue + " to " + newValue);
    return newValue;
}

    // Creates a new Object
var obj = new Object ();

if (obj.watch) {
        // registers the OnPropertyChange function for the changing of the num property
    obj.watch("num", OnPropertyChange);

        // Define the num property (invokes the OnPropertyChange method)
    obj.num = 1;
        // Modify the value of the num property (invokes the OnPropertyChange method)
    obj.num = 2;
        // Delete the num property (does not invoke the OnPropertyChange method)
    delete obj.num;
        // Redefine the num property (invokes the OnPropertyChange method)
    obj.num = 3;
        // Removes the registered function
    obj.unwatch('num');
        // Modify the value of the num property (does not invoke the OnPropertyChange method)
    obj.num = 5;
}
else {
    alert ("Your browser does not support the watch method!");
}
Did you find this example helpful? yes no

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.
<head>
    <script type="text/javascript">
        // Adds a getter and a setter to the Date object
        var d = Date.prototype;

        if (d.__defineGetter__) {
            d.__defineGetter__("year", function() { return this.getFullYear(); });
            d.__defineSetter__("year", function(y) { this.setFullYear(y); });
        } else {
            alert ("Your browser does not support the __defineGetter__ method!");
        }

        // A new instance of the date object
        var date = new Date ();

        function GetYear () {
            alert (date.year);
        }

        function SetYear () {
            var input = document.getElementById ("myInput");
            var newYear = input.value;
            date.year = newYear;
        }
    </script>
</head>
<body>
    <button onclick="GetYear();">Get the current year</button>
    <br />
    <input id="myInput" value="2000"  />
    <button onclick="SetYear ();">Set year</button>

</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content