javascript拦截instanceof
数据抓取进行问题分析很重的一个方法就是拦截(hook),一般的函数属性拦截通过Proxy即可。而对于instanceof则需要使用Symbol.hasInstance。
当一个对象定义Symbol.hasInstance的属性描述符后,就可以拦截到instanceof调用。
Object.defineProperty(Navigator, Symbol.hasInstance, {
value: function(instance) {
debugger;
return true;
}});
console.log(navigator instanceof Navigator);
而对于class增加Symbol.hasInstance方法即可
class MyArray {
static [Symbol.hasInstance](instance) {
return Array.isArray(instance)
}
}
console.log([] instanceof MyArray);
原文
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance