这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要是对 JavaScript 中 instanceof 对于不同的构造器或许都返回 true 进行了详细的解析,需要的朋友可以过来参考下,希望对大家有所帮助
我们知道 instanceof 运算符用来检查对象是否为某构造器的实例。下面列举它返回 true 的各种情景。
1、对象 obj 是通过 new Constructor 创建的,那么 obj instanceof Constructor 为 true
2、如果存在继承关系,那么 子类实例 instanceof 父类 也会返回 true
- function Person(n, a) {
- this.name = n;
- this.age = a;
- }
- var p = new Person('John Backus', 82);
- console.log(p instanceof Person); // true
3、由于 Object 是根类,所有其它自定义类都继承于它,因此 任意构造器的实例 instanceof Object 都返回 true
- function A(){}
- function B(){}
- B.prototype = new A(); // B继承于A
- var b = new B();
- console.log(b instanceof A); // true
- function A() {}
- var a = new A();
- console.log(a instanceof Object); // true
- var str = new String('hello');
- console.log(str instanceof Object); // true
- var num = new Number(1);
- console.log(num instanceof Object); // true
甚至包括构造器自身
4、所有构造器 instanceof Function 返回 true
- function A() {}
- console.log(A instanceof Object); // true
- console.log(String instanceof Object); // true
- console.log(Number instanceof Object); // true
- function A() {}
- console.log(A instanceof Function); // true
- console.log(String instanceof Function); // true
- console.log(Number instanceof Function); // true
以上四点总结为一句话:
如果某实例是通过某类或其子类的创建的,那么 instanceof 就返回 true。或者说某构造函数的原型 存在与对象 obj 的内部原型链上,那么返回 true。即 instanceof 的结果与构造器自身并无直接关系。这在许多语言中都是通用的。
Java 中定义了一个类 Person,实例 p 对于 Person 和 Object 都返回 true
- class Person {
- public String name;
- public int age;
- Person (String n, int a) {
- this.name = name;
- this.age = a;
- }
- public static void main(String[] args) {
- Person p = new Person("John Backus", 82);
- System.out.println(p instanceof Person); // true
- System.out.println(p instanceof Object); // true
- }
- }
Java 中如果存在继承关系,那么 子类实例 instanceof 父类 也返回 true
- // 父类
- class Person {
- public String name;
- public int age;
- Person (String n, int a) {
- name = name;
- age = a;
- }
- }
- // 子类
- public class Man extends Person{
- public String university;
- Man(String n, int a, String s) {
- super(n, a);
- university = s;
- }
- public static void main(String[] args) {
- Man mm = new Man("John Resig", 29, "PKU");
- System.out.println(mm instanceof Man); // true
- System.out.println(mm instanceof Person); // 也是true
- }
- }
知道了这些,JS 中以下的表现就不奇怪了
- // 定义两个构造器
- function A() {}
- function B() {}
- A.prototype = B.prototype = {
- a: 1
- };
- // 分别创建两个不同构造器的实例
- var a = new A();
- var b = new B();
- console.log(a instanceof B); // true
- console.log(b instanceof A); // true
我们看到 a, b 分别是用 A 和 B 创建的,但 a instanceof B 和 b instanceof A 都是 true。即 a 虽然不是用构造器 B 创建的,但仍然返回 true。因为 B.prototype 存在于 a 的内部原型链上。
由于 JS 的动态语言特性,可以在运行时修改原型,因此下面返回 false 也不足为奇了。因为 A.prototype 已经不在 a 的内部原型链中,链条被打断了。
- function A(){}
- var a = new A();
- A.prototype = {}; // 动态修改原型,注意必须在创建a后
- console.log(a instanceof A); // false
注意这么写也打破了上面总结的第一条:对象 obj 是通过 new Constructor 创建的,那么 obj instanceof Constructor 为 true
实际在 ECMAScript 标准中(以 5.1 为准),instanceof 内部实现会调用构造器的内部方法 [[HasInstance]],描述如下
假如 F 是一个函数对象,当 F(V) 执行时,以下步骤将发生:
1、如果 instanceof 左运算元 V 不是对象类型,直接返回 false
- var a, b = 1,
- c = true,
- d = 'hello';
- console.log(a instanceof Object); // false 这里a值为undefined
- console.log(b instanceof Object); // false
- console.log(c instanceof Object); // false
- console.log(d instanceof Object); // false
2/3、取构造器 F 的 prototype 属性,如果不是对象类型,须抛出 TypeError 异常,
- function A(){}
- A.prototype = 1; // A的prototype设为非对象类型
- var a = new A();
- console.log(a instanceof A);
各浏览器抛出的异常提示不同,
Firefox18:
Chrome24:
Safari6:
Opera12:
IE10:
4、不断的执行以下逻辑:将 V 设为内部原型的 V,如果 V 是 null 则返回 false,如果 V 和 O 都指向同一个对象,则返回 true。
来源: http://www.phperz.com/article/17/0423/278043.html