-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathintanceof.js
More file actions
27 lines (26 loc) · 944 Bytes
/
Copy pathintanceof.js
File metadata and controls
27 lines (26 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* eslint-disable no-proto */
/*
* Author : OBKoro1
* Date : 2021-07-30 15:02:10
* LastEditors : OBKoro1 1677593011@qq.com
* LastEditTime : 2024-04-21 14:29:06
* FilePath : /web-basics/src/js/intanceof/intanceof.js
* description : instanceOf实现原理
* koroFileheader VSCode插件
* Copyright (c) 2021 by OBKoro1, All Rights Reserved.
*/
// 作用:一个对象是否在另一个对象的原型链上
// 思路:左边变量的原型链上 有 右边变量的原型, 说明左边对象是继承右边对象的。
function instanceOf(left, right) {
let leftValue = left.__proto__
const rightValue = right.prototype
while (true) {
if (leftValue === null) {
return false // 左边变量的原型链上没找到
}
if (leftValue === rightValue) {
return true // 右边变量的原型在左边变量的原型链上
}
leftValue = leftValue.__proto__ // 找下层原型
}
}