banner
SlhwSR

SlhwSR

热爱技术的一名全栈开发者
github
bilibili

实现instance of

1. 首先,instanceof 用于判断某个构造函数的 prototype 是否存在在某实例对象上。

SmsClass instanceof Function ? "":SmsClass

直接上代码

export function instanceofFn(Left, Constructor) {
   const ConstructorP = Constructor.prototype;
   Left = Left.__proto__;
   //一直向上寻找
   while (true) {
       if (Left === null) {
           return false;
       }
       if (Left === ConstructorP) {
           return true;
       }
       // 持续向上查找原型链
       Left = Left.__proto__;
   }
}
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。