這篇文章,讓你了解 JavaScript 中的原型(基礎篇-圖文)
JavaScript是一門動態語言, 你可以在任何時候向對象上添加屬性,如下
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'Male';
- }
- var studObj1 = new Student();
- studObj1.age = 15;
- alert(studObj1.age); // 15
- var studObj2 = new Student();
- alert(studObj2.age); // undefined
正如上面的實例, age 屬性附加在 studObj1 實例上. 然而 studObj2 實例沒有這個屬性, 因為 age 屬性只在 studObj1 實例上定義了.
那么, 如果想在后期添加一個屬性且能被所有的實例所共享, 該怎么辦? 答案這就今天主角 Prototype.
Prototype 是一個對象, 默認情況下與JavaScript中的任何一個函數或對象有關, 只是唯一區別在于函數的prototype 屬性是可訪問和可修改的,而對象的prototype屬性是不可見的.
默認情況下任何一個函數包含 Prototype 對象, 如下圖:

prototype 對象是一種特殊類型的可枚舉對象, 可以將需要附加屬添加到其上,這些屬性將在其構造函數的所有實例之間共享。
我自己是一名從事了多年開發的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個月整理了一份最適合2019年學習的web前端學習干貨,各種框架都有整理,送給每一位前端小伙伴。
因此, 把上面的示例中使用函數的 prototype 來添加屬性,以便于所有對象中都可以訪問到, 如下:
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'M';
- }
- Student.prototype.age = 15;
- var studObj1 = new Student();
- alert(studObj1.age); // 15
- var studObj2 = new Student();
- alert(studObj2.age); // 15
使用 字面量 或 通過 new關鍵字和構造函數 的方式創建的每一個對象都包含 __proto__ 屬性, 該屬性指向創建此對象的函數的 原型對象.

你可以在谷歌和火狐開發者調試工具中查看該屬性(__proto__) , 根據下面的示例:
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'M';
- }
- var studObj = new Student();
- console.log(Student.prototype); // object
- console.log(studObj.prototype); // undefined
- console.log(studObj.__proto__); // object
- console.log(typeof Student.prototype); // object
- console.log(typeof studObj.__proto__); // object
- console.log(Student.prototype === studObj.__proto__ ); // true
正如上面例子看到, 函數通過 [[函數名稱]].prototype 方式訪問到原型對象. 但是, 對象(實例)并沒有暴露出 prototype 屬性,而是使用 __proto__ 來訪問它.
Object 對象的原型
前面提及到, 原型對象在對象中是不可見. 使用 Object.getPrototypeOf(obj) 方法來訪問實例的原型對象. (這也是推薦方式, __proto__ 并不是標準屬性, 在IE11以下其它瀏覽器中沒有實現).
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'M';
- }
- var studObj = new Student();
- Student.prototype.sayHi= function(){
- alert("Hi");
- };
- var studObj1 = new Student();
- var proto = Object.getPrototypeOf(studObj1);
- // returns Student's prototype object
- alert(proto.constructor);
- // returns Student function
Object 原型對象包含如下 屬性 和 方法
屬性描述constructor返回創建該實例的構造函數__proto__指向創建該實例的構造函數的原型對象.方法描述hasOwnProperty()返回一個布爾值,指示對象是否包含指定的屬性作為該對象的直接屬性,而不是通過原型鏈繼承。isPrototypeOf()返回一個布爾值,指示指定的對象是否位于調用此方法的對象的原型鏈中。propertyIsEnumerable()返回一個布爾值,該布爾值指示指定的屬性是否可枚舉。toLocaleString()返回本地格式的字符串.toString()返回對象字符串形式.valueOf()返回指定對象的原始值.
Chrome 和 Firfox 將對象的原型表示為 __proto__, 而內部引用為 [[Prototype]]. IE不支持,只有IE11包含它.
修改原型
如上所述, 每個對象都能鏈接到函數的原型對象. 如果您更改了函數的原型, 則只有新對象將鏈接到更改后的原型. 所有其他現有對象仍然鏈接到舊的函數原型. 下面實例來演示這個場景:
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'M';
- }
- Student.prototype.age = 15;
- var studObj1 = new Student();
- alert('studObj1.age = ' + studObj1.age); // 15
- var studObj2 = new Student();
- alert('studObj2.age = ' + studObj2.age); // 15
- Student.prototype = { age : 20 };
- var studObj3 = new Student();
- alert('studObj3.age = ' + studObj3.age); // 20
- alert('studObj1.age = ' + studObj1.age); // 15
- alert('studObj2.age = ' + studObj2.age); // 15
使用原型
原型對象被JavaScript引擎用來做兩件事:
查找對象的屬性和方法在JavaScript中實現繼承
- function Student() {
- this.name = 'LeBron James';
- this.gender = 'M';
- }
- Student.prototype.sayHi = function(){
- alert("Hi");
- };
- var studObj = new Student();
- studObj.toString();
在上面的示例, toString() 方法在 Student 中沒有定義, 那么它是如何以及從哪里找到 toString() 的呢?
在這里,原型出現了. 首先, JavaScript 引擎檢查 studObj 是否存在 toString 方法?. 如果沒有找到,那么它使用 studObj 的 __proto__ 鏈接指向 Student函數 的 原型對象. 如果它仍然無法找到它那么它會在往上層并檢查 Object 函數的原型對象,因為所有對象都是從 JavaScript 中的 Object 派生的,并查找 toString() 方法. 因此, 它在Object函數的原型對象中找到 toString()方法,因此我們可以調用 studObj.toString().
查找方式,如下圖所示

上述就是原型基本知識點以及應用.