当前位置:网站首页>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

边栏推荐
- EURA eurui E1000 series inverter uses PID to realize the relevant parameter setting and wiring of constant pressure water supply function
- CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
- Can I choose to open an account for stock trading on flush? Is it safe?
- 分离字符串中的字母和数字并使得字母在前数组在后
- Practical project notes (I) -- creation of virtual machine
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- Accelera Systems Initiative是一个独立的非营利组织
- 最近公共祖先离线做法(tarjan)
- 选择在同花顺上炒股开户可以吗?安全吗?
- NSI脚本的测试
猜你喜欢
随机推荐
leetcode刷题:二叉树02(二叉树的中序遍历)
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
个人炒股怎样开户?安全吗。
pytest合集(2)— pytest运行方式
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
杰理之烧录上层版物料需要【篇】
芭比Q了!新上架的游戏APP,咋分析?
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
运放-滞回(迟滞)比较器全流程实战计算
Is it safe to open an account online? Can a novice open a stock trading account.
Kuberntes云原生实战一 高可用部署架构
选择在同花顺上炒股开户可以吗?安全吗?
Iframe parent-child page communication
从MLPerf谈起:如何引领AI加速器的下一波浪潮
ngnix基础知识
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
js如何获取集合对象中某元素列表
MQ学习笔记
目标检测——Yolo系列
杰理之蓝牙耳机品控和生产技巧【篇】









