除了建立物件,建構函數(constructor)還默默做了另一件重要的事:自動為新物件設定原型物件(prototype object),並存放於 ConstructorFunction.prototype 屬性中。理解這件事,就理解了 JavaScript 繼承的一半。
改用建構函數來建立 b 與 c,此時 a 扮演的角色就是 Foo.prototype:
// 建構函數
function Foo(y) {
// 被 new 呼叫時,這個 y 會成為每個實例自己的屬性
this.y = y;
}
// "Foo.prototype" 存放新建物件的原型參照,
// 因此可以用來定義繼承與共享的屬性和方法
Foo.prototype.x = 0;
// 繼承的方法
Foo.prototype.calculate = function (z) {
return this.x + this.y + z;
};
// 用 Foo 建立 b 與 c
var b = new Foo(10);
var c = new Foo(20);
b.calculate(1);
c.calculate(2);
console.log(
b.__proto__ === Foo.prototype, // true
c.__proto__ === Foo.prototype, // true
// Foo.prototype 自動有一個特殊屬性 constructor,指向建構函數本身
b.constructor === Foo, // true
c.constructor === Foo, // true
Foo.prototype.constructor === Foo, // true
b.calculate === b.__proto__.calculate, // true
b.__proto__.calculate === Foo.prototype.calculate // true
);

從上圖可以看出:每個物件都有一個原型;建構函數 Foo 自己也有一條 __proto__,也就是 Function.prototype,而 Function.prototype 的 __proto__ 又指向 Object.prototype。要特別區分的是,Foo.prototype 只是一個「顯式屬性」,而 b 與 c 的 __proto__ 才是實際的內部連結——兩者指向同一個物件。
這個主題完整說來有兩部分:一是物件導向的一般理論,描述各種 OOP 典範與風格,以及與 ECMAScript 的比較;二是 ECMAScript 的具體實作。了解物件原理之後,接著就是執行環境(runtime program execution),也就是所謂的「執行上下文堆疊」——每一個元素都可以抽象地看成物件。你大概也發現了,在 ECMAScript 裡幾乎處處可見物件的身影。
物件的 constructor 屬性用於回傳建立該物件的函數,也就是我們常說的建構函數。
在 JavaScript 中,每個具有原型的物件都會自動獲得 constructor 屬性。除了 arguments、Enumerator、Error、Global、Math、RegExp 等特殊物件之外,其他內建物件(Array、Boolean、Date、Function、Number、Object、String 等)都具備此屬性,主流瀏覽器皆支援。
語法
object.constructor
回傳值
建立該物件的函數參照。
以下程式中的 [native code] 表示這是 JavaScript 底層的內部實作,無法顯示程式碼細節。
// 字串:String()
var str = "張三";
document.writeln(str.constructor); // function String() { [native code] }
document.writeln(str.constructor === String); // true
// 陣列:Array()
var arr = [1, 2, 3];
document.writeln(arr.constructor); // function Array() { [native code] }
document.writeln(arr.constructor === Array); // true
// 數字:Number()
var num = 5;
document.writeln(num.constructor); // function Number() { [native code] }
document.writeln(num.constructor === Number); // true
// 自訂物件:Person()
function Person(){ this.name = "CodePlayer"; }
var p = new Person();
document.writeln(p.constructor); // function Person(){ this.name = "CodePlayer"; }
document.writeln(p.constructor === Person); // true
// JSON 物件:Object()
var o = { "name" : "張三" };
document.writeln(o.constructor); // function Object() { [native code] }
document.writeln(o.constructor === Object); // true
// 自訂函數:Function()
function foo(){ alert("CodePlayer"); }
document.writeln(foo.constructor); // function Function() { [native code] }
document.writeln(foo.constructor === Function); // true
// 函數的原型
function bar(){ alert("CodePlayer"); }
document.writeln(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }
document.writeln(bar.prototype.constructor === bar); // true
如果只用 constructor 來判斷型別,遇到跨 iframe 或改寫過 prototype 的物件時會失效,較可靠的做法是搭配 Object.prototype.toString.call(obj) 或 instanceof 一起使用。另外,__proto__ 屬於非標準的舊寫法,新程式碼請優先使用 Object.getPrototypeOf() 與 Object.setPrototypeOf()。