代理目标:地图对象

时间:2017-07-12 20:25:11

标签: javascript es6-proxy es6-map

我尝试将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);
})();




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

0 个答案:

没有答案