zoukankan      html  css  js  c++  java
  • instanceof 是如何工作的

    知其然

    JavaScript 提供 instanceof 关键字判断一个对象所属的构造函数。在 js 高级编程中讲到 instanceof 的作用:

    instanceof 操作符,用来测试实例与原型链中出现过的构造函数,只要出现过就会返回 true。

    个人对这句话不是很理解,看下面例子:

    function Person(){
        this.name="tjm";
    }
    
    function Student(){
        Person.call(this);
        this.grade = 1;
    }
    
    Student.prototype = new Person();
    var stu = new Student();
    console.log(stu instanceof Student); //true
    console.log(stu instanceof Person); //true
    

    首先要知道,每个构造函数在没显示指定原型的情况下,会默认分配一个原型对象,该原型对象中包含 constructor 属性指向构造函数,以及 prototype 属性指向 Object 的原型。但通过原型继承,显示指定 Student.prototype = new Person() , 那么构造函数 Student 的原型是 Person 对象,而该原型有一个 prototype 属性指向 Person 构造函数的原型,一个name 等于 tjm 的属性。因此,可以看出,stu 实例的原型链中并没有出现 Student 构造函数,但是stu instanceof Student 依然返回 true,这就让我感到疑惑了。下图展示了 Student 的构造函数的原型链。

    知其所以然

    发现了不解的地方,一个很好的办法就是通过实践来进行检验,多做几次实验测试,就能够发现规律。看下面这段代码。

    function Person(){ //(Object|obj)
        this.name="tjm";
    }
    
    function Student(){ s1
        Person.call(this);
        this.grade = 1;
    }
    Student.prototype = new Person(); // p1
    var stu = new Student();
    Student.prototype = new Person(); // p2 
    console.log(stu instanceof Student); // false
    console.log(stu instanceof Person); // true
    

    上面代码在 stu 实例化之后,再更改 Student 构造函数的原型实例,虽然原型实例还是一个 Person 对象,但是 stu instanceof Student 却为 false 了。下面通过原型对象来分析 instanceof 来判断的值。stu 在实例化时,内部 【prototype】 属性指向的是当时 Student 构造函数所指向的对象,那么 stu 原型对象是 p1 (Person 对象), 但是在 stu 对象实例化之后,Student 构造函数的原型更改为 p2 , 也是一个 Person 对象。虽然 p1,p2 都是 Person 的对象,但是他们却是不同的,拥有不同的内存地址,使用 == 操作符,结果是 false 。通过对上面代码进行分析,可以得出这样的结果:

    obj instanceof Construcotr —> instanceof 操作符是判断 Constructor 构造函数的原型对象是否存在实例 obj 的原型链中,如果存在,那么返回true,否则返回 false。

    根据上面总结再来分析一下代码,stu 实例的原型链中的对象为 p1,p0,Object, 而 Student 的原型对象为 p2 ,因此 stu instanceof Student 当然返回 false。 Student 的原型对象没有在 stu 实例的原型链中。明白了instanceof 操作符真的工作原理,下面我们很容更改 stu 原型链中的对象,使得 stu instancecof Student 返回 true。好在 es5 提供了 __proto__ 属性访问实例的原型。

    function Person(){
        this.name="tjm";
    }
    
    function Student(){
        Person.call(this);
        this.grade = 1;
    }
    var p1 = new Person();
    Student.prototype = p1;
    
    var stu = new Student();
    
    var p2= new Person();
    Student.prototype = p2;
    stu.__proto__ = p2; //通过更改 stu 原型为 p2
    console.log(stu instanceof Student); // true
    console.log(stu instanceof Person); // true
    

    通过 stu.__proto__ = p2; 语句,将 stu 的原型更改为与 Student 构造函数一样的原型对象 p2, 这下 stu instanceof Studetn 结果返回 true。

    小结

    instanceof 操作符作用是:在运行时,构造函数的原型对象是否出现在实例的原型链中,如果出现则返回 true,否则返回false。 切记,一定是运行时,而不是初始化时。

  • 相关阅读:
    Python SocketServer模块
    网络爬虫urllib:request之urlopen
    1.numpy的用法
    21天打造分布式爬虫-简书整站爬取(十)
    21天打造分布式爬虫-下载汽车之家图片(九)
    21天打造分布式爬虫-Crawl类爬取小程序社区(八)
    21天打造分布式爬虫-Spider类爬取糗事百科(七)
    21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)
    21天打造分布式爬虫-多线程下载表情包(五)
    21天打造分布式爬虫-中国天气网和古诗文网实战(四)
  • 原文地址:https://www.cnblogs.com/mingjiatang/p/7478653.html
Copyright © 2011-2022 走看看