Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript模拟实现instanceof #13

Open
GGXXMM opened this issue Aug 8, 2019 · 0 comments
Open

JavaScript模拟实现instanceof #13

GGXXMM opened this issue Aug 8, 2019 · 0 comments
Labels
⭐️ js js knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Aug 8, 2019

instanceof功能

instanceof 运算符用来用来判断引用数据类型。

instanceof原理

首先判断左边变量的隐式原型是否全等于右边的显示原型,相等则返回true,不相等则继续沿着原型链依次向上进行判断,最终都不相等返回false。

模拟instanceof

function instanceOf(left,right) {
    let proto = left.__proto__;
    let prototype = right.prototype
    while(true) {
        if(proto === null) return false
        if(proto === prototype) return true
        proto = proto.__proto__;
    }
}
@GGXXMM GGXXMM changed the title JavaScript模拟实现instanceOf JavaScript模拟实现instanceof Apr 5, 2021
@GGXXMM GGXXMM added the ⭐️ js js knowledge label Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ js js knowledge
Projects
None yet
Development

No branches or pull requests

1 participant