自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

這篇文章,讓你了解 JavaScript 中的原型(基礎篇-圖文)

開發(fā) 前端
prototype 對象是一種特殊類型的可枚舉對象, 可以將需要附加屬添加到其上,這些屬性將在其構造函數(shù)的所有實例之間共享。

 [[279301]]

JavaScript是一門動態(tài)語言, 你可以在任何時候向?qū)ο笊咸砑訉傩?如下

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'Male'
  4.  } 
  5.  var studObj1 = new Student(); 
  6.  studObj1.age = 15; 
  7.  alert(studObj1.age); // 15 
  8.  var studObj2 = new Student(); 
  9.  alert(studObj2.age); // undefined 

正如上面的實例, age 屬性附加在 studObj1 實例上. 然而 studObj2 實例沒有這個屬性, 因為 age 屬性只在 studObj1 實例上定義了.

那么, 如果想在后期添加一個屬性且能被所有的實例所共享, 該怎么辦? 答案這就今天主角 Prototype.

Prototype 是一個對象, 默認情況下與JavaScript中的任何一個函數(shù)或?qū)ο笥嘘P, 只是唯一區(qū)別在于函數(shù)的prototype 屬性是可訪問和可修改的,而對象的prototype屬性是不可見的.

默認情況下任何一個函數(shù)包含 Prototype 對象, 如下圖:

 

這篇文章,讓你了解 JavaScript 中的原型(基礎篇-圖文)

 

prototype 對象是一種特殊類型的可枚舉對象, 可以將需要附加屬添加到其上,這些屬性將在其構造函數(shù)的所有實例之間共享。

我自己是一名從事了多年開發(fā)的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個月整理了一份最適合2019年學習的web前端學習干貨,各種框架都有整理,送給每一位前端小伙伴。

因此, 把上面的示例中使用函數(shù)的 prototype 來添加屬性,以便于所有對象中都可以訪問到, 如下:

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'M'
  4.  } 
  5.  Student.prototype.age = 15; 
  6.  var studObj1 = new Student(); 
  7.  alert(studObj1.age); // 15 
  8.  var studObj2 = new Student(); 
  9.  alert(studObj2.age); // 15 

使用 字面量 或 通過 new關鍵字和構造函數(shù) 的方式創(chuàng)建的每一個對象都包含 __proto__ 屬性, 該屬性指向創(chuàng)建此對象的函數(shù)的 原型對象.

 

這篇文章,讓你了解 JavaScript 中的原型(基礎篇-圖文)

 

你可以在谷歌和火狐開發(fā)者調(diào)試工具中查看該屬性(__proto__) , 根據(jù)下面的示例:

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'M'
  4.  } 
  5.  var studObj = new Student(); 
  6.  console.log(Student.prototype); // object 
  7.  console.log(studObj.prototype); // undefined 
  8.  console.log(studObj.__proto__); // object 
  9.  console.log(typeof Student.prototype); // object 
  10.  console.log(typeof studObj.__proto__); // object 
  11.  console.log(Student.prototype === studObj.__proto__ ); // true 

正如上面例子看到, 函數(shù)通過 [[函數(shù)名稱]].prototype 方式訪問到原型對象. 但是, 對象(實例)并沒有暴露出 prototype 屬性,而是使用 __proto__ 來訪問它.

Object 對象的原型

前面提及到, 原型對象在對象中是不可見. 使用 Object.getPrototypeOf(obj) 方法來訪問實例的原型對象. (這也是推薦方式, __proto__ 并不是標準屬性, 在IE11以下其它瀏覽器中沒有實現(xiàn)).

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'M'
  4.  } 
  5.  var studObj = new Student(); 
  6.  Student.prototype.sayHi= function(){ 
  7.  alert("Hi"); 
  8.  }; 
  9.  var studObj1 = new Student(); 
  10.  var proto = Object.getPrototypeOf(studObj1);  
  11.  // returns Student's prototype object 
  12.   
  13.  alert(proto.constructor);  
  14.  // returns Student function  

Object 原型對象包含如下 屬性 和 方法

屬性描述constructor返回創(chuàng)建該實例的構造函數(shù)__proto__指向創(chuàng)建該實例的構造函數(shù)的原型對象.方法描述hasOwnProperty()返回一個布爾值,指示對象是否包含指定的屬性作為該對象的直接屬性,而不是通過原型鏈繼承。isPrototypeOf()返回一個布爾值,指示指定的對象是否位于調(diào)用此方法的對象的原型鏈中。propertyIsEnumerable()返回一個布爾值,該布爾值指示指定的屬性是否可枚舉。toLocaleString()返回本地格式的字符串.toString()返回對象字符串形式.valueOf()返回指定對象的原始值.

Chrome 和 Firfox 將對象的原型表示為 __proto__, 而內(nèi)部引用為 [[Prototype]]. IE不支持,只有IE11包含它.

修改原型

如上所述, 每個對象都能鏈接到函數(shù)的原型對象. 如果您更改了函數(shù)的原型, 則只有新對象將鏈接到更改后的原型. 所有其他現(xiàn)有對象仍然鏈接到舊的函數(shù)原型. 下面實例來演示這個場景:

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'M'
  4.  } 
  5.  Student.prototype.age = 15; 
  6.  var studObj1 = new Student(); 
  7.  alert('studObj1.age = ' + studObj1.age); // 15 
  8.  var studObj2 = new Student(); 
  9.  alert('studObj2.age = ' + studObj2.age); // 15 
  10.  Student.prototype = { age : 20 }; 
  11.  var studObj3 = new Student(); 
  12.  alert('studObj3.age = ' + studObj3.age); // 20 
  13.  alert('studObj1.age = ' + studObj1.age); // 15 
  14.  alert('studObj2.age = ' + studObj2.age); // 15 

使用原型

原型對象被JavaScript引擎用來做兩件事:

查找對象的屬性和方法在JavaScript中實現(xiàn)繼承

  1. function Student() { 
  2.  this.name = 'LeBron James'
  3.  this.gender = 'M'
  4.  } 
  5.  Student.prototype.sayHi = function(){ 
  6.  alert("Hi"); 
  7.  }; 
  8.  var studObj = new Student(); 
  9.  studObj.toString(); 

在上面的示例, toString() 方法在 Student 中沒有定義, 那么它是如何以及從哪里找到 toString() 的呢?

在這里,原型出現(xiàn)了. 首先, JavaScript 引擎檢查 studObj 是否存在 toString 方法?. 如果沒有找到,那么它使用 studObj 的 __proto__ 鏈接指向 Student函數(shù) 的 原型對象. 如果它仍然無法找到它那么它會在往上層并檢查 Object 函數(shù)的原型對象,因為所有對象都是從 JavaScript 中的 Object 派生的,并查找 toString() 方法. 因此, 它在Object函數(shù)的原型對象中找到 toString()方法,因此我們可以調(diào)用 studObj.toString().

查找方式,如下圖所示

 

這篇文章,讓你了解 JavaScript 中的原型(基礎篇-圖文)

 

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

責任編輯:武曉燕 來源: 今日頭條
相關推薦

2023-05-30 15:06:21

JavaScript屬性開發(fā)

2022-08-09 08:00:55

AWS安全API

2024-01-17 08:18:14

RPAJava技術

2019-09-11 08:52:24

MVCMVPMVVM

2023-07-28 07:14:13

2021-04-16 16:37:23

SpringMVC源碼配置

2023-07-30 15:18:54

JavaScript屬性

2021-01-26 23:46:32

JavaScript數(shù)據(jù)結構前端

2021-03-09 14:04:01

JavaScriptCookie數(shù)據(jù)

2021-06-24 09:05:08

JavaScript日期前端

2023-09-06 14:57:46

JavaScript編程語言

2024-01-30 13:47:45

2024-04-19 14:23:52

SwitchJavaScript開發(fā)

2021-03-05 18:04:15

JavaScript循環(huán)代碼

2021-05-18 08:30:42

JavaScript 前端JavaScript時

2020-11-10 10:48:10

JavaScript屬性對象

2021-01-29 18:41:16

JavaScript函數(shù)語法

2021-02-02 18:39:05

JavaScript

2021-06-04 09:56:01

JavaScript 前端switch

2021-06-29 10:00:02

JavaScriptStorage接口
點贊
收藏

51CTO技術棧公眾號