亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談JavaScript原型鏈

 更新時間:2023年04月24日 08:50:12   作者:前端小蜜蜂來也  
這篇文章主要為大家詳細介紹了JavaScript原型鏈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

instanceof 簡介

在 JavaScript 中,判斷一個變量的類型通常會用 typeof 運算符,在使用 typeof 運算符時采用引用類型存儲值會出現(xiàn)一個問題,無論引用的是什么類型的對象,它都返回 "object"。例如:

var arr = new Array();
console.log( typeof arr ); // object

如果想要確定原型和實例之間的關(guān)系就需要用到 instanceof 操作符, 例如:

var arr = new Array();
var Fn = function() {};
var foo = new Fn();
console.log( arr instanceof Array ); // true
console.log( arr instanceof Object ); // true
console.log( foo instanceof Fn); // true
console.log( foo instanceof Function ); // false
console.log( foo instanceof Object ); // true

Function instanceof Function ?

console.log( String instanceof String );
console.log( Function instanceof Function );
console.log( Function instanceof Object );
console.log( Object instanceof Function );
console.log( Object instanceof Object );

要解釋這個問題就需要了解 1.JavaScript 語言規(guī)范中是如何定義 instanceof 運算符的,2.JavaScript 原型繼承機制。

instanceof 運算符的定義

在 ECMAScript-262 中 instanceof 運算符的定義是這樣的。

12.9.4 Runtime Semantics: InstanceofOperator(O, C)

The abstract operation InstanceofOperator(O, C) implements the generic algorithm for determining if an object O inherits from the inheritance path defined by constructor C. This abstract operation performs the following steps:

1. If Type(C) is not Object, throw a TypeError exception.
2. Let instOfHandler be GetMethod(C,@@hasInstance).
3. ReturnIfAbrupt(instOfHandler).
4. If instOfHandler is not undefined, then
a. Return ToBoolean(Call(instOfHandler, C, ?O?)).
5. If IsCallable(C) is false, throw a TypeError exception.
6. Return OrdinaryHasInstance(C, O).
NOTE Steps 5 and 6 provide compatibility with previous editions of ECMAScript that did not use a @@hasInstance method to define the instanceof operator semantics. If a function object does not define or inherit @@hasInstance it uses the default instanceof semantics.

7.3.19 OrdinaryHasInstance (C, O)

The abstract operation OrdinaryHasInstance implements the default algorithm for determining if an object O inherits from the instance object inheritance path provided by constructor C. This abstract operation performs the following steps:

1. If IsCallable(C) is false, return false.
2. If C has a [[BoundTargetFunction]] internal slot, then
    a. Let BC be the value of C's [[BoundTargetFunction]] internal slot.
    b. Return InstanceofOperator(O,BC) (see 12.9.4).
3. If Type(O) is not Object, return false.
4. Let P be Get(C, "prototype").
5. ReturnIfAbrupt(P).
6. If Type(P) is not Object, throw a TypeError exception.
7. Repeat
    a. Let O be `O.[[GetPrototypeOf]]()`.
    b. ReturnIfAbrupt(O).
    c. If O is null, return false.
    d. If SameValue(P, O) is true, return true.

官網(wǎng)的定義非常晦澀,上面的翻譯成代碼大概就是:

function instanceOf( L, R ) { //L 表示左表達式,R 表示右表達式
    var P = R.prototype; // 取 R 的顯示原型
    L = L.__proto__; // 取 L 的隱式原型
    while ( true ) { 
        if ( L === null ) return false;
        if ( P === L ) return true; 
        L = L.__proto__; 
    } 
}

再直接點的表達就是 instanceof 會一直在 obj 的原型鏈上查找,直到找到右邊那個構(gòu)造函數(shù)的 prototype 屬性,或者為 null 的時候才停止。

類似:

obj.__proto__.__proto__ ... = Obj.prototype

instanceof 會一直沿著隱式原型鏈 __proto__ 向上查找直到 obj.__proto__.__proto__ ...... === Obj.prototype 為止,如果找到則返回 true,也就是 obj 為 Obj 的一個實例。否則返回 false,obj 不是 Obj 的實例。

JavaScript 原型繼承機制

原型與原型鏈

在 JavaScript 每個函數(shù)都有一個 prototype 屬性,該屬性存儲的就是原型對象。JavaScript 構(gòu)造函數(shù)的繼承都是通過 prototype 屬性, 真正的原型鏈的實現(xiàn)是通過 __proto__ 實現(xiàn)的,__proto__其實是指向‘父類’的 prototype 屬性。例如:

var Foo = function() {}
var foo = new Foo;
console.log(foo.__proto__ === Foo.prototype) // true
console.log(Foo.__proto__ === Function.prototype) // true

原型繼承

JavaScript 是單繼承的,Object.prototype 是原型鏈的頂端,所有對象從它繼承了包括 valueOf、toString 等等方法和屬性。

Object 本身是構(gòu)造函數(shù),繼承了 Function.prototype。 Function 也是對象,繼承了 Object.prototype。

Object instanceof Object

ObjectL = Object, ObjectR = Object; 

R = ObjectR.prototype = Object.prototype 
L = ObjectL.__proto__ = Function.prototype 

R != L 
// 循環(huán)查找 L 是否還有 __proto__ 
L = Function.prototype.__proto__ = Object.prototype 

R == L 
// 返回 true

String instanceof String

StringL = String, StringR = String;

R = StringR.prototype = String.prototype
L = StringL.__proto__ = Object.prototype

R != L
// 循環(huán)查找 L 是否還有 __proto__ 
L = Object.prototype.__proto__ = null
// 返回 false

一切皆對象?

常常說 JavaScript 中一切皆對象,那么就有這樣一個問題了:

'string'.__proto__ === String.prototype // true
'string' instanceof String // false

按照上面的推導,'string' instanceof String 應該為 true,但是我們得到的卻是 false。 其實問題的關(guān)鍵在于:

console.log(typeof 'string'); // string

'string' 并不是一個 object 對象,MDN 上對 instanceof 的定義是:

The instanceof operator tests whether an object in its prototype chain has the prototype property of a constructor.

 這樣又有一個問題了,既然字符串不是對象那為什么有對象才有的屬性和方法呢?

var s1 = "string";
var s2 = s1.substring(2);

為了便于操作基本類型值,ECMAScript 還提供了 3 個特殊的引用類型: Boolean、Number 和 String。 實際上,每當讀取一個基本類型值的時候,后臺就會創(chuàng)建一個對應的基本包裝類型的對象,從而讓我們 能夠調(diào)用一些方法來操作這些數(shù)據(jù)。

《JavaScript高級程序設計》中是這么解釋的:

上面的例子其實當?shù)诙写a訪問 s1 時,訪問過程處于一種讀取模式,也就是要 從內(nèi)存中讀取這個字符串的值。而在讀取模式中訪問字符串時,后臺都會自動完成: (1) 創(chuàng)建 String 類型的一個實例; (2) 在實例上調(diào)用指定的方法; (3) 銷毀這個實例。

可以將以上三個步驟想象成是執(zhí)行了下列 ECMAScript 代碼。

var s1 = new String("some text");
var s2 = s1.substring(2);
s1 = null;

《Javascript權(quán)威指南》里說:

其實(包裝對象)在實現(xiàn)上并不一定創(chuàng)建或銷毀這個臨時對象,然而整個過程看起來是這樣的。

這樣 Boolean、Number 是一樣的邏輯。還剩下兩種基本類型:null 和 undefined。

undefined 當我們對變量只聲明沒有初始化時,輸出為 undefined,typeof undefined 返回的是 undefined 也不是 object 類型,所以 undefined 并不是任何對象的實例。

null 表示的是空對象,雖然 typeof null 是 object,但是 null 和 undefined 一樣并沒有任何屬性和方法,在 instanceof 定義中也有判斷,如果類型不是 object(這個類型判斷并不是跟 typeof 返回值一樣),就返回 false。

寫在最后

如果這篇文章對你有幫助,請不要吝嗇你手中的贊??!你的鼓勵將是我不斷分享的動力!??

到此這篇關(guān)于淺談JavaScript原型鏈的文章就介紹到這了,更多相關(guān)JavaScript原型鏈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在網(wǎng)頁里看flash的trace數(shù)據(jù)的js類

    在網(wǎng)頁里看flash的trace數(shù)據(jù)的js類

    我的js類jdhcn.js中的一個flashDebug方法
    2009-01-01
  • Javascript中的getter和setter初識

    Javascript中的getter和setter初識

    最近在工作中遇到了getter和setter,getter 是一種獲得屬性值的方法,setter是一種設置屬性值的方法。下面這篇文章主要給大家介紹了關(guān)于Javascript中g(shù)etter和setter的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • JavaScript設計模式之調(diào)停者模式實例詳解

    JavaScript設計模式之調(diào)停者模式實例詳解

    這篇文章主要介紹了JavaScript設計模式之調(diào)停者模式,詳細分析了調(diào)停者模式的概念、原理、優(yōu)缺點并結(jié)合javascript實例形式給出了相關(guān)使用技巧,需要的朋友可以參考下
    2018-02-02
  • JavaScript性能優(yōu)化之小知識總結(jié)

    JavaScript性能優(yōu)化之小知識總結(jié)

    JavaScript的性能問題不容小覷,這就需要我們開發(fā)人員在編寫JavaScript程序時多注意一些細節(jié),本文給大家介紹javascript性能優(yōu)化之小知識總結(jié),需要的朋友可以參考下
    2015-11-11
  • Day.js常用方法集合(附各種事件格式的轉(zhuǎn)換)

    Day.js常用方法集合(附各種事件格式的轉(zhuǎn)換)

    dayjs是一個輕量的處理時間和日期的JavaScript庫,下面這篇文章主要給大家介紹了關(guān)于Day.js常用方法的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • JavaScript判斷是否為數(shù)字的4種方法及效率比較

    JavaScript判斷是否為數(shù)字的4種方法及效率比較

    這篇文章主要介紹了JavaScript判斷是否為數(shù)字的4種方法及效率比較,本文直接給出判斷方法實現(xiàn)代碼及運行效率效果圖,方便大家選擇使用,需要的朋友可以參考下
    2015-04-04
  • JS實現(xiàn)復制功能

    JS實現(xiàn)復制功能

    本文主要介紹了JS實現(xiàn)復制功能的實例,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • BootStrap Table前臺和后臺分頁對JSON格式的要求

    BootStrap Table前臺和后臺分頁對JSON格式的要求

    Bootstrap是一款前端非常流行的框架,其中的表格更為大家經(jīng)常使用。下面通過本文給大家介紹BootStrap Table前臺和后臺分頁對JSON格式的要求,一起看看吧
    2017-06-06
  • uniapp movable-area應用

    uniapp movable-area應用

    這篇文章主要為大家介紹了uniapp movable-area應用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • 微信小程序?qū)崿F(xiàn)基于三元運算驗證手機號/姓名功能示例

    微信小程序?qū)崿F(xiàn)基于三元運算驗證手機號/姓名功能示例

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)基于三元運算驗證手機號/姓名功能,涉及三元運算符的判定及字符串正則驗證相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01

最新評論