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.
В | Copy Code |
---|---|
function testObject(ob) { if (ob.constructor == String) print("Object is a String."); else if (ob.constructor == MyFunc) print("Object is constructed from MyFunc."); else print("Object is neither a String or constructed from MyFunc."); } // A constructor function. function MyFunc() { // Body of function. } var x = new String("Hi"); testObject(x) var y = new MyFunc; testObject(y); |
The output of this program is:
В | Copy Code |
---|---|
Object is a String. Object is constructed from MyFunc. |