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

边栏推荐
- Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)
- Penetration tools - trustedsec's penetration testing framework (PTF)
- Practical project notes (I) -- creation of virtual machine
- deb文件安装
- 芭比Q了!新上架的游戏APP,咋分析?
- 2022年低压电工考试试题及答案
- 关联线探究,如何连接流程图的两个节点
- MySQL清空表数据
- [multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
- 深度学习 常见的损失函数
猜你喜欢

Halcon知识:三维重构的一个尝试

工控设备安全加密的意义和措施
![[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)](/img/bf/524e78473625a31c024783ccec8d46.png)
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)

杰理之、产线装配环节【篇】

在技术升级中迎合消费者需求,安吉尔净水器“价值战”的竞争之道

杰理之蓝牙耳机品控和生产技巧【篇】

300 linear algebra Lecture 4 linear equations

联想电脑怎么连接蓝牙耳机?

GCC编译

leetcode刷题:栈与队列05(逆波兰表达式求值)
随机推荐
目標檢測——Yolo系列
PHP 读取ini或env类型配置
There are four ways to write switch, you know
leetcode刷题:栈与队列02(用队列实现栈)
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
leetcode刷题:二叉树02(二叉树的中序遍历)
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
js数组拼接的四种方法[通俗易懂]
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
柒微自动发卡系统源码
网上开户是安全的吗?新手可以开炒股账户吗。
pytest合集(2)— pytest运行方式
Architect graduation summary
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
ngnix基础知识
EMC-电路保护器件-防浪涌及冲击电流用
深度学习 神经网络基础
leetcode刷题:栈与队列06(前 K 个高频元素)
新牛牛盲盒微信小程序源码_支持流量变现,带完整素材图片
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)