02 # 手写 instanceof 的原理

instanceof 干什么的?

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

instanceof 可以判断一个对象是否属于某个类

<script>
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }

    Person.prototype.sayHello = function () {
        console.log("hello kaimo");
    };

    // 寄生组合继承
    function Student(name, age, className) {
        // 继承 Person 私有属性
        Person.call(this, name, age);
        this.className = className;
    }

    // 继承 Person 公有属性:使用 `Object.create()` 来解决多次调用父类构造函数问题
    Student.prototype = Object.create(Person.prototype);
    // 由于修改了构造器 `Student.prototype`,`Student.prototype.constructor` 已经不是 Student
    // 为了避免误解,手动重设 `Student.prototype.constructor` 属性
    // 这样通过 new Student 创建的实例的 constructor 又可以正确取到 Student
    Student.prototype.constructor = Student;
    console.log(Student.prototype.constructor);

    let p1 = new Person("kaimo", 313);
    console.log("p1---->", p1 instanceof Person);
    let s = new Student("kaimo-s", 18, "手写课堂");
    console.log(s);
    s.sayHello();
    console.log("s--Student-->", s instanceof Student);
    console.log("s--Person-->", s instanceof Person);
</script>

在这里插入图片描述

手写 instanceof

<script>
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }

    Person.prototype.sayHello = function () {
        console.log("hello kaimo");
    };

    function Student(name, age, className) {
        Person.call(this, name, age);
        this.className = className;
    }

    Student.prototype = Object.create(Person.prototype);
    Student.prototype.constructor = Student;

    // 使用 kaimoInstanceof 模拟 instanceof 运算符
    function kaimoInstanceof(className, obj) {
        let fp = className.prototype; // className 的原型对象
        let cp = obj.__proto__; // obj 是 className 实例的原型对象
        // cp 为 null 结束循环,Object.prototype.__proto__ = null
        while (cp) {
            if (fp === cp) {
                return true;
            }
            // 往原型链上继续找
            cp = cp.__proto__;
        }
        return false;
    }

    let p1 = new Person("kaimo", 313);
    console.log("p1---->", kaimoInstanceof(Person, p1));
    let s = new Student("kaimo-s", 18, "手写课堂");
    console.log(s);
    s.sayHello();
    console.log("s--Student-->", kaimoInstanceof(Student, s));
    console.log("s--Person-->", kaimoInstanceof(Person, s));
</script>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯小默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值