Author | 王平安 |
---|---|
pingan8787@qq.com | |
博 客 | www.pingan8787.com |
微 信 | pingan8787 |
每日文章推荐 | https://github.com/pingan8787/Leo_Reading/issues |
最近开始在整理ES6/ES7/ES8/ES9
的知识点(已经上传到 我的博客 上),碰到一些知识点是自己已经忘记(用得少的知识点),于是也重新复习了一遍。
这篇文章要复习的 instanceof
是我在整理过程中遇到的,那就整理下来吧,不然容易忘记。
要是哪里写得不妥,欢迎各位大佬指点。
1.定义
instanceof
运算符用于测试构造函数的prototype
属性是否出现在对象的原型链中的任何位置。 —— MDN
简单理解为:instanceof
可以检测一个实例是否属于某种类型。
比如:1
2
3
4
5
6
7function F (){
// ...
}
let a = new F ();
a instanceof F; // true
a instanceof Object; // true 后面介绍原因
还可以在继承关系中用来判断一个实例是否属于它的父类型。
比如:1
2
3
4
5
6
7
8
9function F (){};
function G (){};
function Q (){};
G.prototype = new F(); // 继承
let a = new G();
a instanceof F; // true
a instanceof G; // true
a instanceof Q; // false
2.使用方法
语法为: object instanceof constructor
。
object
: 需要测试的函数constructor
: 构造函数
即:用instanceof
运算符来检测constructor.prototype
是否存在参数object
的原型链。1
2
3
4
5
6
7
8function F (){};
function G (){};
let a = new F ();
a instanceof F; // true 因为:Object.getPrototypeOf(a) === F.prototype
a instanceof Q; // false 因为:F.prototype不在a的原型链上
a instanceof Object; // true 因为:Object.prototype.isPrototypeOf(a)返回true
F.prototype instanceof Object; // true,同上
注意:
a instanceof F
返回true
以后,不一定永远都都返回为true
,F.prototype
属性的值有可能会改变。- 原表达式
a
的值也会改变,比如a.__proto__ = {}
之后,a instanceof F
就会返回false
了。
检测对象是不是特定构造函数的实例:1
2
3
4
5
6
7
8// 正确
if (!(obj instanceof F)) {
// ...
}
// 错误 因为
if (!obj instanceof F); // 永远返回false
// 因为 !obj 在instanceof之前被处理 , 即一直使用一个布尔值检测是否是F的实例
3.实现instanceof
1 | /** |
4.instanceof 与 typeof 对比
相同:instanceof
和typeof
都能用来判断一个变量的类型。
区别:instanceof
只能用来判断对象、函数和数组,不能用来判断字符串和数字等:1
2
3
4
5
6
7
8
9
10
11let a = {};
let b = function () {};
let c = [];
let d = 'hi';
let e = 123;
a instanceof Object; // true
b instanceof Object; // true
c instanceof Array; // true
d instanceof String; // false
e instanceof Number; // false
typeof
:用于判断一个表达式的原始值,返回一个字符串。1
2
3
4typeof 42; // "number"
typeof 'blubber'; // "string"
typeof true; // "boolean"
typeof aa; // "undefined"
一般返回结果有:
- number
- boolean
- string
- function(函数)
- object(NULL,数组,对象)
- undefined。
判断变量是否存在:
不能使用:1
2
3
4if(a){
//变量存在
}
// Uncaught ReferenceError: a is not defined
原因是如果变量未定义,就会报未定义的错,而应该使用:1
2
3if(typeof a != 'undefined'){
//变量存在
}