Export (0) Print
Expand All

constructor Property

Specifies the function that creates an object.

object.constructor

object

Required. 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 JScript objects except the arguments, Enumerator, Error, Global, Math, RegExp, Regular Expression, and VBArray objects. The constructor property contains a reference to the function that constructs instances of that particular object.

Class-based objects do not have a constructor property.

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");
print(testObject(x));

var y = new MyFunc;
print(testObject(y));

The output of this program is:

Object is a String.
Object is constructed from MyFunc.

Community Additions

ADD
Show:
© 2015 Microsoft