未捕获的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);
})();

问题:如何将Map
设为Proxy
的目标?