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

手写API-instanceof #3

Open
dark9wesley opened this issue Feb 20, 2021 · 0 comments
Open

手写API-instanceof #3

dark9wesley opened this issue Feb 20, 2021 · 0 comments

Comments

@dark9wesley
Copy link
Owner

function myInstanceof(left, right){
      //如果是基础数据类型,直接返回false
      if(typeof left !== 'object' || left === null) return false;
      //获取实例的原型对象,这里采用Object.getPrototypeOf
      let proto = Object.getPrototypeOf(left);

      while(true){
        //查找到原型链的顶端还没找到,返回false
        if(proto === null) return false;
        //在原型链中找到构造函数的prototype,返回true
        if(proto === right.prototype) return true;
        //当前步骤未找到,将proto重新赋值为proto的原型,重新执行循环
        proto = Object.getPrototypeOf(proto);
      }

 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant