标签:color 代理 ons cti type return OLE 调用 targe
let target = { name: "Tom", age: 24 }; let handler = { get: function(target, key) { console.log("getting " + key); return target[key]; // 不是target.key }, set: function(target, key, value) { console.log("setting " + key); console.log(target[key]) target[key] = value; } }; let proxy = new Proxy(target, handler); proxy.name; // 实际执行 handler.get console.log(proxy.age) proxy.age = 25; // 实际执行 handler.set
例子2:创建元素
//拦截 const DOM=new Proxy({},{ get(target,property){ console.log(property) return function(attr={},...children){ const el=document.createElement(property) for(let key of Object.keys(attr)){ el.setAttribute(key,attr[key]) } for(let child of children){ if(typeof child==‘string‘){ child=document.createTextNode(child) } el.appendChild(child) } return el; } } }) let oDiv=DOM.div({id:‘div1‘,class:‘aaa‘},‘我是Div‘,‘呵呵‘,DOM.a({href:‘http://baidu‘},‘百度‘)) console.log(oDiv)
标签:color 代理 ons cti type return OLE 调用 targe
原文地址:https://www.cnblogs.com/yanloveyue/p/11286839.html