Javascript原型鏈及instanceof原理詳解
instanceof:用來判斷實(shí)例是否是屬于某個(gè)對象,這個(gè)判斷依據(jù)是什么呢?
首先,了解一下javascript中的原型繼承的基礎(chǔ)知識(shí):
javascript中的對象都有一個(gè)__proto__屬性,這個(gè)是對象的隱式原型,指向該對象的父對象的原型(prototype)。顯式的原型對象使用prototype,但是Object.prototype.proto=null;
判斷某個(gè)對象a是否屬于某個(gè)類A的實(shí)例,可以通過搜索原型鏈。
實(shí)例對象屬性查找順序是:實(shí)例對象內(nèi)部---->構(gòu)造函數(shù)原型鏈---->實(shí)例對象父對象的原型鏈。
//繼承機(jī)制 function A(){ } A.prototype.name=’licui’; function B(){ } B.prototype = new A(); var a = new A(); var b = new B(); //b.name = ’hello’; console.log(’A:’,A); console.log(’B:’,B); console.log(’a:’,a); console.log(’b:’,b); console.log(’A.prototype’,A.prototype); console.log(’B.prototype’,B.prototype); console.log(’a._proto_’,a.__proto__); console.log(’b._proto_’,b.__proto__); console.log(’a instanceof A:’,a instanceof A); console.log(’a instanceof Object:’,a instanceof Object); console.log(’b instanceof B:’,b instanceof B); console.log(’b instanceof A:’,b instanceof A); console.log(’b instanceof Object:’,b instanceof Object);
執(zhí)行結(jié)果
constructor:是原型prototype上的屬性,實(shí)例上不具有該屬性。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
