instanceof机制和实现
NOxONE 7/21/2022  js
# 1. instanceof 机制
- 获取对象的
__proto__和判断类型的prototype - 遍历对象上的所有原型直到==判断类型的
prototype返回true - 若遍历到尽头也就是 null 了还是没结果返回
false 
function instanceof(left, right) {
  for (left = left.__proto__; left; left = left.__proto__) {
    if (left === right.prototype) return true
  }
  return false
}
# 2. [Symblol.hasInstance]方法重写
更精确的来说a instanceof B实际会执行B[Symbol.hasInstance](a)
// instanceof机制
class A {
	static [Symbol.hasInstance](instance) {
		for (instance = instance.__proto; instance; instance = instance.__proto__) {
			if (instance === this) return true
		}
		return false
	}
}
new A() instanceof A // true
// 重写
class Person {
	static [Symbol.hasInstance](instance) {
		return instance instanceof Array
	}
}
;[1, 2, 3] instanceof Person // true
new Person() instanceof Person // false
