``` js function _instanceof(leftValue, rightValue) { const leftValueType = typeof leftValue if (leftValueType !== 'object' && leftValueType !== 'function') return false rightValue = rightValue.prototype leftValue = leftValue.__proto__ while (true) { if (leftValue === null) return false if (leftValue === rightValue) return true leftValue = leftValue.__proto__ } } // 测试代码 console.log(_instanceof(Object, Function)) console.log(_instanceof(Function, Object)) console.log(_instanceof(Function, Function)) console.log(_instanceof(new Number(123), Number)) console.log(_instanceof(123, Number)) console.log(Object instanceof Function) console.log(Function instanceof Object) console.log(Function instanceof Function) console.log(new Number(123) instanceof Number) console.log(123 instanceof Number) ```
Activity
[-]手写题:如何实现 instanceof 方法?[/-][+]如何实现 instanceof 方法?[/+]RainyNight9 commentedon Jun 14, 2022
luckept commentedon Jun 14, 2022
洪总这里有个小问题

luckept commentedon Jun 14, 2022
老哥这么写也有点小问题

Hongbusi commentedon Jun 14, 2022
增加了一个判断来确定
leftValue值的类型。 @luckeptluckept commentedon Jun 14, 2022
棒!