0

I'm trying to set a Map as the target of a Proxy, but I'm getting the following error:

Uncaught TypeError: this.proxy.family.set is not a function

I believe the error is related to both .set() method in both the Map and Proxy objects.

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);
})();

Question: How can I set a Map as a Proxy's target?

  • You successfully did set the map as the target of the proxy. Only it's your property `get` and `set` traps that make no sense. You should get a log message right before the exception. – Bergi Jul 12 '17 at 21:23
  • @Bergi yeah your right I was just coming to that realization. –  Jul 12 '17 at 21:31
  • @Bergi I think I should make my own traps do to statement restrictions like the `in` to trigger the `has` etc.. what do you think? –  Jul 12 '17 at 21:40
  • I think you should not use a proxy or traps at all. What is your [actual problem](https://meta.stackexchange.com/q/66377)? – Bergi Jul 12 '17 at 21:42
  • I'm trying to trigger events when a object in a Map changes. –  Jul 12 '17 at 21:51
  • Well, `addFamilyMember` is the only method that changes your map, so just trigger the event there. – Bergi Jul 12 '17 at 21:56
  • @Bergi yeah my problem is I'm using a map when I should be using a Set -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set –  Jul 12 '17 at 22:06
  • I don't follow. Using `Set` instead of `Map` doesn't change the proxy problem or helps triggering events on changes – Bergi Jul 12 '17 at 22:10

0 Answers0