Home › JavaScript Tutorial › JS meta and proxy
JS meta and proxy
Intercept object operations.
A Proxy wraps an object and traps reads and writes. Reflect performs the default action. Symbol makes unique keys.
Example code
<pre id='o'></pre>
<script>
const p=new Proxy({},{
get:(t,k)=>k in t?t[k]:'no '+String(k),
set(t,k,v){if(typeof v!=='number')throw new TypeError('numbers only');return Reflect.set(t,k,v);}
});
p.age=30;let t=p.age+', '+p.name;
try{p.x='a'}catch(e){t+=', '+e.message}
o.textContent=t;
</script>