Bagikan melalui


constructor Property

Specifies the function that creates an object.

object.constructor

Arguments

  • object
    Required. The name of an object or function.

Remarks

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.

Example

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.

Requirements

Version 2

Applies To:

Array Object| Boolean Object| Date Object| Function Object| Number Object| Object Object| String Object

See Also

Reference

prototype Property

Change History

Date

History

Reason

July 2009

Modified example.

Content bug fix.

March 2009

Modified example.

Information enhancement.