javascript中instanceof運(yùn)算符的用法詳解
概述
instanceof運(yùn)算符用來判斷一個(gè)構(gòu)造函數(shù)的prototype屬性所指向的對(duì)象是否存在另外一個(gè)要檢測(cè)對(duì)象的原型鏈上
語法
obj instanceof Object;//true 實(shí)例obj在不在Object構(gòu)造函數(shù)中
描述
instanceof 運(yùn)算符用來檢測(cè) constructor.prototype 是否存在于參數(shù) object 的原型鏈上。
實(shí)例
1.instanceof的普通的用法,obj instanceof Object 檢測(cè)Object.prototype是否存在于參數(shù)obj的原型鏈上。
Person的原型在p的原型鏈中
function Person(){}; var p =new Person(); console.log(p instanceof Person);//true
2.繼承中判斷實(shí)例是否屬于它的父類
Student和Person都在s的原型鏈中
function Person(){}; function Student(){}; var p =new Person(); Student.prototype=p;//繼承原型 var s=new Student(); console.log(s instanceof Student);//true console.log(s instanceof Person);//true
3.復(fù)雜用法
這里的案例要有熟練的原型鏈的認(rèn)識(shí)才能理解
function Person() {} console.log(Object instanceof Object); //true //第一個(gè)Object的原型鏈:Object=> //Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype //第二個(gè)Object的原型:Object=> Object.prototype console.log(Function instanceof Function); //true //第一個(gè)Function的原型鏈:Function=>Function.__proto__ => Function.prototype //第二個(gè)Function的原型:Function=>Function.prototype console.log(Function instanceof Object); //true //Function=> //Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype //Object => Object.prototype console.log(Person instanceof Function); //true //Person=>Person.__proto__=>Function.prototype //Function=>Function.prototype console.log(String instanceof String); //false //第一個(gè)String的原型鏈:String=> //String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype //第二個(gè)String的原型鏈:String=>String.prototype console.log(Boolean instanceof Boolean); //false //第一個(gè)Boolean的原型鏈:Boolean=> //Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype //第二個(gè)Boolean的原型鏈:Boolean=>Boolean.prototype console.log(Person instanceof Person); //false //第一個(gè)Person的原型鏈:Person=> //Person.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype //第二個(gè)Person的原型鏈:Person=>Person.prototype
總結(jié)
對(duì)應(yīng)上述規(guī)范做個(gè)函數(shù)模擬A instanceof B:
function _instanceof(A, B) { var O = B.prototype;// 取B的顯示原型 A = A.__proto__;// 取A的隱式原型 while (true) { //Object.prototype.__proto__ === null if (A === null) return false; if (O === A)// 這里重點(diǎn):當(dāng) O 嚴(yán)格等于 A 時(shí),返回 true return true; A = A.__proto__; } }
到此這篇關(guān)于javascript中instanceof運(yùn)算符的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript入門學(xué)習(xí)第五篇 js函數(shù)
上篇文章講了js中對(duì)象和數(shù)組的一些方法。 這章我們先說說函數(shù),然后來點(diǎn)實(shí)戰(zhàn)。2008-07-07圖文詳解Heap Sort堆排序算法及JavaScript的代碼實(shí)現(xiàn)
這篇文章以圖文詳解Heap Sort堆排序算法及JavaScript的代碼實(shí)現(xiàn),堆排序算法基于類二叉樹的堆數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2016-05-05關(guān)于不同頁面之間實(shí)現(xiàn)參數(shù)傳遞的幾種方式討論
下面小編就為大家?guī)硪黄P(guān)于不同頁面之間實(shí)現(xiàn)參數(shù)傳遞的幾種方式討論。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Ajax解決跨域之設(shè)置CORS響應(yīng)頭實(shí)現(xiàn)跨域案例詳解
這篇文章主要介紹了Ajax解決跨域之設(shè)置CORS響應(yīng)頭實(shí)現(xiàn)跨域案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07javascript學(xué)習(xí)筆記(四)function函數(shù)部分
本文主要介紹了函數(shù)的調(diào)用方式、返回函數(shù)的函數(shù)、創(chuàng)建匿名函數(shù)、javascript創(chuàng)建動(dòng)態(tài)函數(shù)、回調(diào)函數(shù)、方法和函數(shù)的區(qū)別、js全局函數(shù)、函數(shù)的幾個(gè)作用、prototype屬性、高階函數(shù),非常實(shí)用,有需要的參考下2014-09-09Node.js生成HttpStatusCode輔助類發(fā)布到npm
本篇文章小編為大家介紹利用Node.js為Node.js生成HttpStatusCode輔助類并發(fā)布到npm,有需要的朋友可以參考一下2013-04-04