指定创建一个对象的函数。
object.constructor
- object
必选。对象或函数的名称。
constructor 属性是每个具有原型的对象的原型成员。这包括除了 arguments、Enumerator、Error、Global、Math、RegExp、Regular Expression 和 VBArray 对象以外的所有内部 JScript 对象。constructor 属性包含了对某种函数的引用,此种函数构造了特定对象的实例。
基于类的对象没有 constructor 属性。
示例下面的示例阐释了 constructor 属性的用法。
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);
该程序的输出为:
Object is a String. Object is constructed from MyFunc.要求
应用于:
Array 对象 | Boolean 对象 | Date 对象 | Function 对象 | Number 对象 | Object 对象 | String 对象