function myInstanceof(son, fu) {
let proto = Object.getPrototypeOf(son), // 获取对象的原型
prototype = fu.prototype; // 获取构造函数的 prototype 对象
// 判断构造函数的 prototype 对象是否在对象的原型链上
// 不断获取son的原型
while (true) {
if (!proto) return false;
if (proto === prototype) return true;
proto = Object.getPrototypeOf(proto);
}
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
instanceOf实现
最新推荐文章于 2024-07-28 20:27:10 发布