Sitemap
Kei Cheng

Kei Cheng

Because it's there. George Mallory

Javascript Prototype

9 min readFeb 22, 2023

--

Javascript 除了基本型別之外,其餘所有皆是物件。

當初 Brendan Eich 開發 Javascript 過程中,為了避免 JS 過於像是 C++ 繼承那樣複雜,因此 JS 一直都沒有 Class 可以使用,即便 ES6 的 Class 也只是語法糖並非是真正的 Class。但為了能將物件能夠串連起來,使用了 prototype 的概念將物件作繼承的動作。每當一個物件被建立時,同時也會自動生成 __proto__ 與 Constructor。

Get Kei Cheng’s stories in your inbox

Join Medium for free to get updates from this writer.

每當建立一個物件時,同時也會跟 Object 繼承屬性與方法。當中 __proto__ 就是與上層 Object 作為關聯的方式。而最頂級的 Object.__proto__ 則會是 null。

var o1 = {};
console.log(o1);
/*
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
__proto__: Object
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
*/

console.log(o1.__proto__ === Object.prototype);
// true,o1 的上層即是 Object
console.log(o1.__proto__.__proto__);
// null,o1 的上層是最頂級的 Object,再上一層就不會有任何東西

從建構函式來看原型鍊

當建立函式時是從 Function 去繼承,因此每次建立函式時都會有 constructor 與 __proto__ 兩個屬性。

// constructor
function Person(name, age) {
this.name = name;
this.age = age;
this.log = function () {
console.log(this.name + ', age:' + this.age);
}
}
console.log(Person.__proto__ === Function.prototype)
// true,Person 由 Function 繼承

console.log(Person.__proto__.__proto__ === Object.prototype)
// true,函式也是從物件繼承

當要建立 Person 這個物件時,可以使用 new 這個關鍵字來建立物件。但這時候要注意的是被 new 出來的是物件,而不是函式了。

var kei = new Person('Kei', 30);
console.log(kei.__proto__ === Person.prototype);
// true,從 Person 建立實例,所以 kei 的上層是 Person 這符合預期

console.log(kei.__proto__.__proto__ === Function.prototype);
// false,ㄟ ~怎麼上層不是 Function 了?

console.log(kei.__proto__.__proto__ === Object.prototype);
// true

new

new 這個關鍵字用來建立實體時,會執行幾件事情:

  • 創建一個新物件,暫且稱為 o
  • 把 o 的 __proto__ 指向到 Person.prototype 建立原型鍊
  • 把 o 作為上下文呼叫 Person 這個建構函式
  • 回傳 o
var o = new Object();

// 讓 o 繼承原型鍊
o.__proto__ = Person.prototype;

// 執行建構函式
Constructor.apply(o, arguments);

// 回傳建立好的物件
return o;

所以無論是從哪裡 new 出一個新物件,上層的 __proto__ 都會是 Object.prototype

let m = new Map();
console.log(a.__proto__ === Map.prototype) // true
console.log(a.__proto__.__proto__ === Object.prototype) // true

instanceof

a instanceof b,用來判斷 a 是否為 b 的實例

console.log(kei instanceof Person) // true
console.log(kei instanceof Object) // true
console.log(kei instanceof Function) // false

這裡有一個特別的現象,Object 與 Function 互為實例。原因是 Object.prototype 本身也是建構函式,因此也會指向到 Function,這裡就是原型鍊最容易搞混淆的地方。

console.log(Object.__proto__);
// ƒ () { [native code] }
// 可能會以為作為最頂級的 Object 的 __proto__ 會是 null
// 如果作為原型鍊的確是如此,若單一看 Object.__proto__
// 則是建構函式

console.log(Function instanceof Object); // true
console.log(Object instanceof Function); // true

// Function 的 __proto__ 會指向 Function.prototype
// 而 Function.prototype 的 __proto__ 會指向 Object.prototype
console.log(Function.__proto__ === Function.prototype); // true
console.log(Function.__proto__.__proto__ === Object.prototype); //true

// Object 的 __proto__ 會指向 Function.prototype
console.log(Object.__proto__ === Function.prototype); // true

Prototype

function Person(name, age) {
this.name = name;
this.age = age;
this.log = function () {
console.log(this.name + ', age:' + this.age);
}
}

上述 Person 範例中可以另外觀察到 name 與 age 在每一次實體化都會是不同的,但 log 這個方法做的事情相同,每個實體之間應該可以共享,這種情況就會佔兩份空間。為了讓建構函式可以共享方法,這時候就可以利用 prototype 來讓每個 Person 實例達成共享 log。

function Person(name, age) {
this.name = name;
this.age = age;
}

Person.prototype.log = function () {
console.log(this.name + ', age:' + this.age);
}

var kei = new Person('Kei', 30);
var jin = new Person('Jinjin', 30);
console.log(kei.log === jin.log) // true
// 此時 log 這個方法就會是 Person 的共享方法

console.log(kei.__proto__ === Person.prototype)
// true,kei 的上一層就會是 Person

console.log(kei.__proto__.__proto__ === Object.prototype);
// true,再上一層即是頂層的 Object,若再上一層就會顯示 null

JS 原生物件也可以透過 prototype 去擴充或修改原生的方法,但是不建議這麼做。

Array.prototype.last = function () {
return this[this.length - 1];
};

console.log([1,2,3].last()) // 3

constructor

每一個 prototype 都會有個 constructor。若是建構函式,原型中的 constructor 指的就是自己。

// Person 的 constructor 就是 Person
console.log(Person.prototype.constructor === Person); // true
console.log(Person.prototype.hasOwnProperty('constructor')); // true

Object.getPrototypeOf(obj)

透過這個方法可以取得物件的原型鍊來源。

let obj1 = {};
console.log(Object.getPrototypeOf(obj1) === Object.prototype)
// true
// 每當透過物件實字或物件建構式建立一個物件都是從 Object.prototype 繼承原型鍊

let obj2 = Object.create(obj1);
console.log(Object.getPrototypeOf(obj2) === obj1);
// true

console.log(Object.getPrototypeOf(kei) === Person.prototype);
// true

console.log(Object.getPrototypeOf(Person) === Function.prototype);
// true

Object.setPrototypeOf(obj, prototype)

物件的原型鍊是可以被更改的,但是不建議使用這個方法。更改原型鍊對 JS 引擎來說是一項很慢的操作。如果要改變物件原型鍊的新對象,建議使用 Object.create()

class Human {}
class SuperHero {}

Object.setPrototypeOf(SuperHero.prototype, Human.prototype);
// 範例來自 mdn 文件

--

--

Kei Cheng
Kei Cheng

Because it's there. George Mallory