網站首頁 編程語言 正文
文章目錄
- 1-1 instance主要作用及使用
- 1-2 insInstanceOf原理
- 1-3 手擼代碼及測試階段
1-1 instance主要作用及使用
判斷一個實例是否屬于某種類型
let person = function(){
}
let no = new person();
no instanceof person; // true
1-2 insInstanceOf原理
在了解原理及手擼代碼之前,需要了解JS原型鏈:
JS原型鏈
1、 主要實現原理: 只要右邊變量的prototype在左邊變量的原型鏈上即可
- so,instanceof在查找的過程中會遍歷左邊變量的原型鏈,直到找到右邊變量的prototype
- 查找失敗,返回false,即左邊變量并非右邊變量的實例
2、
function new_instanceOf(leftValue, rightValue) {
let rightValue = rightValue.prototype; // 取右表達式的prototyoe值
leftValue = leftValue.__proto__;
while (true) {
if (leftValue === rightProto) {
return true;
}
if (leftValue === null) {
return false;
}
leftVaule = leftVaule.__proto__ ;
}
}
1-3 手擼代碼及測試階段
function instance_of(l,r) {
let rProto = r.prototype;
let lValue = l.__proto__;
while(true) {
if (lValue === null) {
return false;
}
if (lValue === rProto) {
return true;
}
lValue = lValue.__proto__;
}
}
// 開始測試
var a = []
var b = {}
function Foo(){}
var c = new Foo()
function child(){}
function father(){}
child.prototype = new father()
var d = new child()
console.log(instance_of(a, Array)) // true
console.log(instance_of(b, Object)) // true
console.log(instance_of(b, Array)) // false
console.log(instance_of(a, Object)) // true
console.log(instance_of(c, Foo)) // true
console.log(instance_of(d, child)) // true
console.log(instance_of(d, father)) // true
原文鏈接:https://blog.csdn.net/hannah2233/article/details/125481680
- 上一篇:CSS解決未知高度垂直居中
- 下一篇:SQL語句優化詳解
相關推薦
- 2022-11-24 AOP?Redis自定義注解實現細粒度接口IP訪問限制_Redis
- 2022-12-03 FFmpeg?Principle學習new_video_stream添加視頻輸出流_Android
- 2022-04-08 深入理解Golang的反射reflect示例_Golang
- 2022-12-25 深入了解Go語言中goioc框架的使用_Golang
- 2022-03-27 C#?Razor語法規則_C#教程
- 2022-01-06 Spring Aware接口詳解
- 2022-10-20 Android?PowerManagerService?打開省電模式_Android
- 2022-12-07 C++11?成員函數作為回調函數的使用方式_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支