案例:
let arr = [];
let proxyer = new Proxy(arr, {});
console.log(proxyer instanceof Proxy);
console.log(proxyer instanceof Array);
console.log(proxyer.__proto__.constructor);
new Proxy的实例和被代理的taget保持相同的引用类型,Proxy的实例的原型上也没有所谓的Proxy构造函数
那么如何判断一个元素是proxy类型呢?
使用Symbol.toStringTag
通俗理解就是可以改变Object.prototype.toString 的执行结果
({[Symbol.toStringTag]: 'Foo'}.toString())
class Collection {
get [Symbol.toStringTag]() {
return 'xxx';
}
}
let x = new Collection();
Object.prototype.toString.call(x)
最终解决方案
Proxy = new Proxy(Proxy, {
construct: function (target, argumentsList) {
const result = new target(...argumentsList);
const originToStringTag = Object.prototype.toString.call(result).slice(1,-1).split(' ')[1]
result[Symbol.toStringTag] = 'Proxy-' + originToStringTag;
return result;
},
});
let a = new Proxy([],{})
|