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

第二十题:instanceof与typeof #20

Open
Ray-56 opened this issue Sep 4, 2019 · 1 comment
Open

第二十题:instanceof与typeof #20

Ray-56 opened this issue Sep 4, 2019 · 1 comment
Labels
JavaScript 解释型编程语言

Comments

@Ray-56
Copy link
Owner

Ray-56 commented Sep 4, 2019

并实现一个instanceof

@Ray-56 Ray-56 added the JavaScript 解释型编程语言 label Sep 4, 2019
@MMmaXingXing
Copy link

共有特性

typeof与instacne都是js变量类型检测的运算符

typeOf

在js中,typeof是检测基础类型的神器,对于所有引用类型的返回都是object;

instanceOf

当检测某个实例是不是当前对象的实例的时候,就到了instance大展拳脚的时刻了。

instanceOf的实现

// 左边表达式和右边表达式
instanceOf (L, R) {
    L = L.__proto__;
    R = R.prototype;
    While(true) {
        if (L == null) return false;
        if (L == R) return true;
        L = L.__proto__
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript 解释型编程语言
Projects
None yet
Development

No branches or pull requests

2 participants