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