作者:奥兰度

    1. let toString = Object.prototype.toString.bind()
    2. function myInstanceof (object, constructor) {
    3. let tag = toString(constructor)
    4. if (
    5. tag === '[object Boolean]' ||
    6. tag === '[object String]' ||
    7. tag === '[object Number]' ||
    8. tag === '[object Undefined]' ||
    9. tag === '[object Null]' ||
    10. tag === '[object Symbol]' ||
    11. tag === '[object BigInt]' ||
    12. ) {
    13. throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not an object`)
    14. }
    15. if (tag === '[object Array]' || tag === '[object Object]') {
    16. throw new Error(`Uncaught TypeError: Right-hand side of 'instanceof' is not callable`)
    17. }
    18. let proto = Object.getPrototypeOf(object)
    19. let prototype = constructor.prototype
    20. while(proto) {
    21. if (proto === prototype) {
    22. return true
    23. }
    24. proto = Object.getPrototypeOf(proto)
    25. }
    26. return false
    27. }

    作者:安静

    1. function myInstanceof(instance: any, oringin: any): boolean {
    2. if(instance == null ) return false; // undefined null
    3. const type = typeof instance;
    4. if(type !== 'object' && type !== 'function') return false; // 值类型
    5. let tempInstance = instance; // 防止修改 instance
    6. while(tempInstance) {
    7. if(tempInstance.__proto__ === oringin.prototype) return true; // 匹配上了
    8. // 未匹配
    9. tempInstance = tempInstance.__proto__; // 顺着原型链,向上查找
    10. }
    11. return false;
    12. }
    13. console.log(myInstanceof({}, Object)); // true
    14. console.log(myInstanceof([], Object)); // true
    15. console.log(myInstanceof([], Array)); // true
    16. console.log(myInstanceof({}, Array)); // false
    17. console.log(myInstanceof('abc', String)); // false
    18. console.log(myInstanceof(100, Number)); // false
    19. console.log(myInstanceof(null, Object)); // false
    20. console.log(myInstanceof(undefined, Object)); // false
    21. function fn(){}
    22. console.log(myInstanceof(fn, Function), 'fn'); // true
    23. console.log(myInstanceof(fn, Object), 'fn'); // true
    24. class Foo {};
    25. const f1 = new Foo();
    26. console.log(myInstanceof(f1, Foo)); // true
    27. console.log(myInstanceof(f1, Object)); // true
    28. console.log(myInstanceof(f1, Array)); // false

    作者:寥寥

    1. function myInstanceof(left, right) {
    2. //这里先用typeof来判断基础数据类型,如果是,直接返回false
    3. if (typeof left !== 'object' || left === null) return false;
    4. // 获取对象的原型
    5. let proto = Object.getPrototypeOf(left)
    6. // 获取构造函数的prototype对象
    7. let prototype = right.prototype
    8. // 判断构造函数的prototype对象是否在对象的原型链上
    9. while (true) {
    10. if (!proto) return false;
    11. if (proto === prototype) return true;
    12. proto = Object.getPrototypeOf(proto);
    13. }
    14. }

    作者:不更新

    1. /**
    2. *
    3. * 用于检测构造函数prototype的属性是否出现在某个实例对象的原型链上
    4. * @param construction 构造函数
    5. * @param newObj 实例对象
    6. */
    7. const instanceofIfNeeded = (construction, newObj) => {
    8. let constructionProto = Object.getPrototypeOf(construction);
    9. let newObjProto = Object.getPrototypeOf(newObj);
    10. while (true) {
    11. if (newObjProto == null) {
    12. return false;
    13. }
    14. if (constructionProto === newObjProto) {
    15. return true;
    16. }
    17. newObjProto = Object.getPrototypeOf(newObjProto);
    18. }
    19. };

    作者:Leo

    1. function myInstanceof(left, right) {
    2. if(typeof right !== 'object') throw new Error('Right-hand side of 'instanceof' is not an object');
    3. let proto = left.__proto__;
    4. if (!proto) return false;
    5. return right.prototype === proto ? true : myInstanceof(proto, right);
    6. }

    作者
    作者
    作者
    作者