Proxy使用詳解

來源 |?https://www.cnblogs.com/chuaWeb/p/13676000.html
通用
2、代理實(shí)例中沒有指定的handler,實(shí)際就是操作原對象target:實(shí)例:打開控制臺(tái)查看:http://jsrun.net/3RLKp/edit
let target = function(){return 'ddd'}let proxy = new Proxy(target, {});proxy.prototype.age = 12console.log(proxy.prototype === target.prototype) // true
MDN上有一個(gè)實(shí)例比較特別【拓展構(gòu)造函數(shù)】,來看一下:在線例子:http://jsrun.net/cRLKp/edit
function extend(sup, base) {var descriptor = Object.getOwnPropertyDescriptor(base.prototype, 'constructor');base.prototype = Object.create(sup.prototype);var handler = {construct: function(target, args) {var obj = Object.create(base.prototype);this.apply(target, obj, args);return obj;},apply: function(target, that, args) {sup.apply(that, args);base.apply(that, args);}};var proxy = new Proxy(base, handler);descriptor.value = proxy;Object.defineProperty(base.prototype, 'constructor', descriptor);return proxy;}var Person = function(name) {this.name = name;};var Boy = extend(Person, function(name, age) {this.age = age;});Boy.prototype.gender = 'M';var Peter = new Boy('Peter', 13);console.log(Peter.gender); // "M"console.log(Peter.name); // "Peter"console.log(Peter.age); // 13

執(zhí)行 var Peter = new Boy('Peter', 13);
new操作進(jìn)入到handler.construct,里面的上下文環(huán)境this綁定在handler(可以查看MDN文檔描述)。直接調(diào)用this.apply進(jìn)入handler.apply執(zhí)行。new操作執(zhí)行完畢之后的數(shù)據(jù)結(jié)構(gòu)

巧妙利用原型鏈和代理
handler形參中的receiver
但是如果對象繼承了代理對象的情況,如下:
"use strict"const proxy = new Proxy({}, {get: function(target, prop, receiver) {if(proxy === receiver){console.log('receiver為proxy')}else if(obj === receiver){console.log('receiver為obj')}else{console.log('receiver不為proxy也不為obj')}return 'chua';}});proxy.dd // receiver為proxylet obj = Object.create(proxy);obj.msg // receiver為obj
obj是obj.msg觸發(fā)handler的原始調(diào)用(源頭)
handler.set
set必須返回一個(gè)boolean類型
注意:返回的數(shù)據(jù)如果不是boolean類型,會(huì)轉(zhuǎn)換成布爾類型,假值包括:undefined,null,false, +0, -0, NaN, ""?:實(shí)例
const target = {msg: "hello"};const handler = {set: function(target, prop, value, receiver){target[prop] = value// return true}};const proxy = new Proxy(target, handler);proxy.msg = 'wow' // Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'msg'
1、如果相應(yīng)的目標(biāo)對象屬性是不可寫的數(shù)據(jù)屬性,則無法將屬性的值更改為與相應(yīng)目標(biāo)對象屬性的值不同的值。實(shí)例:嚴(yán)格模式
var obj = {}Object.defineProperty(obj, 'year', {// configurable: false, 默認(rèn)false// writable: false, 默認(rèn)falsevalue: 2})Object.defineProperty(obj, 'class', {configurable: true,// writable: false, 默認(rèn)falsevalue: 'chua'})var proxy = new Proxy(obj, {set(target, prop, val){target[prop] = valreturn true}})proxy.card = 'sdf' // 設(shè)置成功proxy.year = 10 // Uncaught TypeError: Cannot assign to read only property 'year' of objectproxy.class = 'dd' // Uncaught TypeError: Cannot assign to read only property 'class' of object
var obj = {}const definereactive = function(data, key, val) {Object.defineProperty(data, key, {get: function(){return val},set: undefined // 應(yīng)該設(shè)置成下面這個(gè)正確的函數(shù)// function(newVal) {// val = newVal;// }});}definereactive(obj, 'year', obj.year)var proxy = new Proxy(obj, {set(target, prop, val){target[prop] = valreturn true}})obj.year = 20 // Uncaught TypeError: Cannot set property year of #proxy.year = 30 // Uncaught TypeError: Cannot set property year of #
復(fù)雜對象
const target = {info: {name: 'chua',age: 18}};const handler = {set: function(target, prop, value, receiver){console.log('in handler.set', target, prop, value, receiver)target[prop] = valuereturn true}};const proxy = new Proxy(target, handler);proxy.info.name = 'chua1989' // 沒有進(jìn)入handler.set, 需要直接更改info屬性才行console.log(proxy.info.name) // chua1989
handler.has
1、target的某屬性為不可配置,則該屬性不能被代理隱藏(即handle.has不能返回false):?在線運(yùn)行
var obj = {}Object.defineProperty(obj, 'year', {configurable: false,value: 2})var proxy = new Proxy(obj, {has: function(target, prop) {console.log('called: ' + prop);return false;}})console.log('year' in proxy); // Uncaught TypeError: 'has' on proxy: trap returned falsish for property 'year' which exists in the proxy target as non-configurable
var obj = { year: 2}Object.preventExtensions(obj);var proxy = new Proxy(obj, {has: function(target, prop) {console.log('called: ' + prop);return false;}})console.log('a' in proxy); // 不存在的屬性沒有問題console.log('year' in proxy); // Uncaught TypeError: 'has' on proxy: trap returned falsish for property 'year' but the proxy target is not extensible
handler.construct
const p = new Proxy({}, {construct: function(target, argumentsList, newTarget) {return function(){};}});new p(); // proxy.html:16 Uncaught TypeError: p is not a constructor
const p = new Proxy(function() {}, {construct: function(target, argumentsList, newTarget) {return 1;}});new p(); // TypeError is thrown
const p = new Proxy(function() {}, {construct: function(target, argumentsList, newTarget) {return function(){};}});new p();
handler.deleteProperty
var p = new Proxy({}, {deleteProperty: function(target, prop) {console.log('called: ' + prop);return false;}});delete p.a; // "called: a"
var obj = {}Object.defineProperty(obj, 'a', {configurable: false})var p = new Proxy(obj, {deleteProperty: function(target, prop) {console.log('called: ' + prop);return true;}});delete p.a; // "called: a" // Uncaught TypeError: 'deleteProperty' on proxy: trap returned truish for property 'a' which is non-configurable in the proxy target
handler.defineProperty
如果目標(biāo)對象的某個(gè)屬性不可寫(writable)或不可配置(configurable),則defineProperty()方法不得改變這兩個(gè)設(shè)置(這是Object.defineProperty的特性)。
handler.getPrototypeOf
getPrototypeOf() 方法返回的不是對象也不是 null。
目標(biāo)對象是不可擴(kuò)展的,且 getPrototypeOf() 方法返回的原型不是目標(biāo)對象本身的原型。
handler.isExtensible
Object.isExtensible(proxy) 必須同Object.isExtensible(target)返回相同值。也就是必須返回true或者為true的值,返回false和為false的值都會(huì)報(bào)錯(cuò)。
handler.ownKeys
1、目標(biāo)對象上不存在的屬性
2、屬性名為 Symbol 值
3、不可遍歷(enumerable)的屬性
1、ownKeys 的結(jié)果必須是一個(gè)數(shù)組.
2、數(shù)組的元素類型要么是一個(gè) String ,要么是一個(gè) Symbol.
3、結(jié)果列表必須包含目標(biāo)對象的所有不可配置(non-configurable )、自有(own)屬性的key.
4、如果目標(biāo)對象不可擴(kuò)展,那么結(jié)果列表必須包含目標(biāo)對象的所有自有(own)屬性的key,不能有其它值.
handler.preventExtensions
handler.setPrototypeOf
如果你不想為你的對象設(shè)置一個(gè)新的原型,你的handler's的setPrototypeOf方法可以返回false,也可以拋出異常。
var handlerReturnsFalse = {setPrototypeOf(target, newProto) {return false;}};var newProto = {}, target = {};var p1 = new Proxy(target, handlerReturnsFalse);Object.setPrototypeOf(p1, newProto); // throws a TypeErrorReflect.setPrototypeOf(p1, newProto); // returns false
if(Reflect.setPrototypeOf(p1, newProto)){...}
handler的屬性方法中的this
const target = new Date();const handler = {};const proxy = new Proxy(target, handler);proxy.getDate();// TypeError: this is not a Date object.

評論
圖片
表情
