当前位置:网站首页>instanceof原理与实现
instanceof原理与实现
2022-06-21 05:25:00 【SignalFire】
一、instanceof概述
(1)instanceof是做什么的
a instanceof b 判断实例a是否属于b类型
(2)如何判断是否属于b类型
每一个对象都有一个__proto__属性指向原型对象的prototype属性,
原型对象的__proto__属性也会指向它的原型对象的prototype属性
因此,如果实例a是b类型,那么a的原型链上必然有b的prototype属性
二、实现instanceof
function myInstanceOf(a,b){
let valA = a.__proto__;
let valB = b.prototype;
while(1){
if(valA == null) return false;
if(valA == valB) return true;
valA = valA.__proto__;
}
}
function Person(name){
this.name = name;
}
let per = new Person("Asia");
let obj = {}
console.log(myInstanceOf(per,Person));//true
console.log(per instanceof Person);//true
console.log(myInstanceOf(obj,Person));//false
console.log(obj instanceof Person);//fasle
let arr = [1,2,3];
console.log(myInstanceOf(arr,Array));//true
console.log(arr instanceof Array);//true
三、特殊比较
console.log(Function instanceof Function);//true
console.log(Object instanceof Object);//true
console.log(String instanceof String);//false
console.log(Number instanceof Number);//false
边栏推荐
- Glycosylated albumin research - abbexa ELISA kit to help!
- launcher切頁動畫
- Abnova β-半乳糖苷酶重组单克隆抗体方案
- 根据类中某个属性去重
- Abnova 6-ketone-pgf1- α ELISA Kit instructions
- Yyds dry goods inventory solution Huawei machine test: conversion between integer and IP address
- How to remove the prohibition of copying and pasting set by teachers
- Seven design principles
- Redis 缓存穿透、缓存击穿、缓存雪崩
- In the NLP field, what are the most practical and effective skills or ideas used in text classification, NER, QA, generation, relationship extraction, etc?
猜你喜欢

Deeply explore the technical characteristics of maker Education

Qualcomm snapdragon processor DSP
![[graduation season] nine year program ape has something to say](/img/35/d0cfa14e435714e31e860c751576a6.png)
[graduation season] nine year program ape has something to say

Glycosylated albumin research - abbexa ELISA kit to help!

Randomly create circular, triangular or rectangular objects, store them in the array, and calculate the area and perimeter of each shape

常见的请求方式和请求头参数

Clothing sales mall system based on ssm+mysql+bootstrap+jsp

mac os MAMP 安装redis 报错问题 ./common.h:12:10: fatal error: ‘zend_smart_str.h‘ file not found

At mode explanation and code implementation of Seata's four modes

MAC NAMP PRO 自带mysql 5.7 设置 sql_model 去掉 ONLY_FULL_GROUP_BY 无效 可能是这样原因,
随机推荐
解读复合型新工科机器人教育模式
Renren fast start prompt process env. NODE_ ENV
Dayjs get the last day of the first day of the current month the last day of the first day of the current year
Randomly create circular, triangular or rectangular objects, store them in the array, and calculate the area and perimeter of each shape
Global and Chinese markets of RFID (radio frequency identification) smart cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
Abnova 6-酮-PGF1-α ELISA 试剂盒说明书
Summary and practice of knowledge map construction technology
[graduation season] nine year program ape has something to say
Improve the comprehensive quality of teachers and students in the concept of maker Education
Global and Chinese markets of X-ray generators 2022-2028: Research Report on technology, participants, trends, market size and share
基于SSM+Bootstrap+MySQL+Thymeleaf的汽车维修管理系统
五分钟搞定软件设计师下午题
File contains vulnerability - allow_ url_ Fopen and allow_ url_ Include details
Abbexa 一抗、二抗、蛋白质等生物试剂方案
软件研发团队效能提升从点滴做起
Interprocess communication ----aidl
Steam education subject integrates the intelligent scene of AR
5.watch方法的实现
[reading papers] sorting out the learning methods of trans series knowledge representation
Privacy sandbox helps enterprises: how privacy technology protects user data and promotes business growth