当前位置:网站首页>Manually implement function isinstanceof (child, parent)
Manually implement function isinstanceof (child, parent)
2022-07-01 21:36:00 【Yiyao Bingo】
List of articles
1-1 instance Main functions and use
Determine whether an instance belongs to a certain type
let person = function(){
}
let no = new person();
no instanceof person; // true
1-2 insInstanceOf principle
Before understanding the principle and hand code , Need to know JS Prototype chain : JS Prototype chain
1、 The main implementation principle : Just the variables on the right prototype On the prototype chain of the left variable
- so,instanceof In the process of searching, we will traverse the prototype chain of variables on the left , Until you find the... Of the variable on the right prototype
- To find the failure , return false, That is, the left variable is not an instance of the right variable
2、
function new_instanceOf(leftValue, rightValue) {
let rightValue = rightValue.prototype; // Take... Of the right expression prototyoe value
leftValue = leftValue.__proto__;
while (true) {
if (leftValue === rightProto) {
return true;
}
if (leftValue === null) {
return false;
}
leftVaule = leftVaule.__proto__ ;
}
}
1-3 Manual code and test stage
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__;
}
}
// Start testing
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

边栏推荐
- 多个张量与多个卷积核做卷积运算的输出结果
- 从20s优化到500ms,我用了这三招
- BC35&BC95 ONENET MQTT(旧)
- 浏览器tab页之间的通信
- 薛定谔的日语学习小程序源码
- 以飞地园区为样本,看雨花与韶山如何奏响长株潭一体化发展高歌
- Learn white box test case design from simple to deep
- 新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码
- Customize the insertion of page labels and realize the initial search of similar address books
- 【Opencv450】HOG+SVM 与Hog+cascade进行行人检测
猜你喜欢
随机推荐
【Opencv450】HOG+SVM 与Hog+cascade进行行人检测
关联线探究,如何连接流程图的两个节点
人才近悦远来,望城区夯实“强省会”智力底座
AirServer手机第三方投屏电脑软件
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
喜马拉雅自研网关架构演进过程
Data analysts sound tall? Understand these points before you decide whether to transform
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
如果浏览器被意外关闭,react怎么缓存用户填写的表单?
杰理之、产线装配环节【篇】
On the usage of a magic function
图片拼图微信小程序源码_支持多模板制作和流量主
【STM32】STM32CubeMX教程二–基本使用(新建工程点亮LED灯)
月入1W+的自媒体达人都会用到的运营工具
考虑关系的图卷积神经网络R-GCN的一些理解以及DGL官方代码的一些讲解
同花顺股票开户选哪个券商好手机开户是安全么?
三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
架构师毕业总结
leetcode刷题:栈与队列07(滑动窗口最大值)







