Export (0) Print
Expand All

constructor Property (JavaScript)

Specifies the function that creates an object.

object.constructor

The required object is the name of an object or function.

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. The constructor property contains a reference to the function that constructs instances of that particular object.

The following example illustrates the use of the constructor property.

function testObject(ob)
{
    if (ob.constructor == String)
        return ("Object is a String.");
    else if (ob.constructor == MyFunc)
        return ("Object is constructed from MyFunc.");
    else
        return ("Object is neither a String nor constructed from MyFunc.");
}

// A constructor function.
function MyFunc() {
    // Body of function.
}

var x = new String("Hi");
document.write(testObject(x));
document.write ("<br />");

var y = new MyFunc;
document.write(testObject(y));

The output of this program is as follows.

Object is a String.
Object is constructed from MyFunc.

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards. See Version Information.

Applies To: Array Object (JavaScript)| Boolean Object (JavaScript)| Date Object (JavaScript)| Function Object (JavaScript)| Math Object (JavaScript)| Number Object (JavaScript)| Object Object (JavaScript)| String Object (JavaScript)

Community Additions

Show:
© 2014 Microsoft