【重温基础】instanceof运算符
栏目: JavaScript · 发布时间: 6年前
内容简介:最近开始在整理这篇文章要复习的要是哪里写得不妥,欢迎各位大佬指点。
最近开始在整理 ES6/ES7/ES8/ES9
的知识点(已经上传到我的博客 上),碰到一些知识点是自己已经忘记(用得少的知识点),于是也重新复习了一遍。
这篇文章要复习的 instanceof
是我在整理过程中遇到的,那就整理下来吧,不然容易忘记。
要是哪里写得不妥,欢迎各位大佬指点。
1.定义
instanceof
运算符用于测试构造函数的 prototype
属性是否出现在对象的原型链中的任何位置。 —— MDN
简单理解为: instanceof
可以检测一个实例是否属于某种类型。
比如:
function F (){ // ... } let a = new F (); a instanceof F; // true a instanceof Object; // true 后面介绍原因 复制代码
还可以在 继承关系 中用来判断一个实例是否属于它的父类型。
比如:
function 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
的原型链。
function 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
了。
检测对象是不是特定构造函数的实例:
// 正确 if (!(obj instanceof F)) { // ... } // 错误 因为 if (!obj instanceof F); // 永远返回false // 因为 !obj 在instanceof之前被处理 , 即一直使用一个布尔值检测是否是F的实例 复制代码
3.实现instanceof
/** * 实现instanceof * @param obj{Object} 需要测试的对象 * @param fun{Function} 构造函数 */ function _instanceof(obj, fun) { let f = fun.prototype; // 取B的显示原型 obj = obj.__proto__; // 取A的隐式原型 while (true) { //Object.prototype.__proto__ === null if (obj === null) return false; if (f === obj) // 这里重点:当 f 严格等于 obj 时,返回 true return true; obj = obj.__proto__; } } 复制代码
4.instanceof 与 typeof 对比
相同:
instanceof
和 typeof
都能用来判断一个变量的类型。
区别:
instanceof
只能用来判断对象、函数和数组,不能用来判断字符串和数字等:
let 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
:用于判断一个表达式的原始值,返回一个字符串。
typeof 42; // "number" typeof 'blubber'; // "string" typeof true; // "boolean" typeof aa; // "undefined" 复制代码
一般返回结果有:
- number
- boolean
- string
- function(函数)
- object(NULL,数组,对象)
- undefined。
判断变量是否存在:
不能使用:
if(a){ //变量存在 } // Uncaught ReferenceError: a is not defined 复制代码
原因是如果变量未定义,就会报 未定义 的错,而应该使用:
if(typeof a != 'undefined'){ //变量存在 } 复制代码
5.参考资料
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- ES6—扩展运算符和rest运算符(6)
- C/C++三元运算符实际上是否具有与赋值运算符相同的优先级?
- Python 运算符
- Python算术运算符
- 004.Python运算符
- JavaScript③运算符
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
About Face 3
Alan Cooper、Robert Reimann、David Cronin / John Wiley & Sons / 2007-5-15 / GBP 28.99
* The return of the authoritative bestseller includes all new content relevant to the popularization of how About Face maintains its relevance to new Web technologies such as AJAX and mobile platforms......一起来看看 《About Face 3》 这本书的介绍吧!