我正在尝试将Map设置为Proxy的目标 ,但出现以下错误:
未捕获的 TypeError:this.proxy.family.set 不是函数
我相信该错误与和对象中的两种.set()
方法有关。Map
Proxy
function FamilyMember(name,type){
this.name = name;
this.type = type;
}
function Person(name){
this.name = name;
this.family = new Map();
this.proxy = {
family: new Proxy(this.family,{
"set":function(target,name){
log(`the target is: ${target}, and the property is: ${name}`);
return true;
},
"get":function(target,name){
log(`the target is: ${target}, and the name is: ${name}`);
return true;
}
})
};
this.addFamilyMember=(member)=>{
if(!this.family.has(member)){
//the problem is here somewhere with the (.set) method
//having to do with the Proxy.constructor as well as the Map.constructor
log(this.proxy.family); //has both (.set) and (.get) in the [[Handler]]
this.proxy.family.set(member,member.name); //error saying this.proxy.family.set is not a function
}
else{
throw `This family member: ${member.name} is already part of the family`;
}
}
}
//INIT
(()=>{
var Cassie = new Person('cassie');
var Holly = new FamilyMember('holly','sister');
var Linds = new FamilyMember('lindsay','sister');
Cassie.addFamilyMember(Holly);
Cassie.addFamilyMember(Linds);
log(Cassie.family);
})();
问题:如何将 a 设置Map
为 aProxy
的目标?