js 的 instanceof 很简单!

it2026-04-22  8

如何判断 instanceof 返回真 只要判断右边的值是否是左边对象的构造函数或原型对象上的构造函数即可 // example var arr = []; 由于: 1. arr.constructor === Array 2. arr.__proto__ === Array.prototype 3. arr.__poto__.proto__ === Object.prototype 所以, 以下都返回true 1. arr instanceof arr.constructor(Array) 2. arr instanceof arr.__proto__.constructor(Array) 3. arr instanceof arr.__proto__.__poto__.constructor(Object) 如果你了解原型链的话,你很快就会得出一些结论: 1. 所有对象 instanceof Object 都会返回 true 2. 所有函数 instanceof Function 都会返回 true

  

转载于:https://www.cnblogs.com/ax-null/p/6966041.html

最新回复(0)