proxy和reflect
Posted anthonyliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了proxy和reflect相关的知识,希望对你有一定的参考价值。
1.proxy
用来拦截在一个对象上的指定操,如:
var engineer = { name: ‘Joe Sixpack‘, salary: 50 }; var interceptor = { //不仅仅有set方法,还有get...等通用的方法 set: function (receiver, property, value) { console.log(property, ‘is changed to‘, value); receiver[property] = value; } }; engineer = new Proxy(engineer, interceptor); engineer.salary = 60; //salary is changed to 60
也可以应用在多继承:
var foo = { foo: function () { console.log("foo") } }; var bar = { bar: function () { console.log("bar") } }; var sonOfFooBar = new Proxy({}, { get: function (target, name) { console.log("target=",target); return target[name] || foo[name] || bar[name]; } }) sonOfFooBar.foo(); //"foo",有foo方法,继承自对象foo sonOfFooBar.bar(); //"bar",也有bar方法,继承自对象bar
以上是关于proxy和reflect的主要内容,如果未能解决你的问题,请参考以下文章