constructor 屬性
更新:2007 年 11 月
指定用來建立物件的函式。
object.constructor
引數
- object
必要項。物件或函式的名稱。
備註
constructor 屬性為每個具有原型之物件的原型成員。這包括所有的內建 JScript 物件,但 arguments、Enumerator、Error、Global、Math、RegExp、Regular Expression 和 VBArray 物件除外。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 物件