proxy和reflect
阿新 • • 發佈:2017-10-03
|| 應用 pro engine ron hang div get off
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