function instanceofFunc(obj, cons) { // 错误判断 构造函数必须是一个function 其他的均报错 if (typeof cons !== 'function') throw new Error('instance error'); if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) return false; // 获取到原型对象 let proto = cons.prototype; // 如果obj的原型对象不是null while (obj.__proto__) { if (obj.__proto__ === proto) return true; obj = obj.__proto__; } return false; } console.log(instanceofFunc(() => {}, Function)); // true
本文作者:前端小毛
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!