This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

constructor

Summary

References the function which created the instance of the Array object.

Syntax

arrayObj.constructor

Return Value

The function object which constructed the Array instance.

Examples

The following example illustrates the use of the constructor property.

var x = new Array();

 if (x.constructor == Array)
     document.write("Object is an Array.");
 else
     document.write("Object is not an Array.");

 // Output:
 // Object is an Array.

View live example

Remarks

The constructor property is a member of the prototype of every object that has a prototype. This includes all intrinsic JavaScript objects except the Global and Math objects. If the JavaScript interpreter falls back to their prototype object, the constructor property references the native Object.prototype.constructor.

var my_obj = Math;
document.write(my_obj.constructor === Math);

// Output: false

The constructor property is only read-only for primitive values such as 1, true and "test".

var my_obj = new Number(100);
my_obj.constructor = String; // overwritten
document.write(my_obj.constructor);

// Output: function String() { [native code] }

my_obj = 100;
my_obj.constructor = String; // can't be overwritten
document.write(my_obj.constructor);

// Output: function Number() { [native code] }

See also

External resources

Specification

ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011

Attributions

  • Microsoft Developer Network: Article