{"id":3138,"date":"2018-11-01T10:48:49","date_gmt":"2018-11-01T02:48:49","guid":{"rendered":"http:\/\/www.daliane.com\/?p=3138"},"modified":"2018-11-02T09:08:47","modified_gmt":"2018-11-02T01:08:47","slug":"es6_proxy_zen_me_shi_yong_quan_pou_xi","status":"publish","type":"post","link":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/es6_proxy_zen_me_shi_yong_quan_pou_xi\/","title":{"rendered":"ES6 Proxy\u600e\u4e48\u4f7f\u7528\u5168\u5256\u6790"},"content":{"rendered":"<p style=\"font-size:28px;color:#00B7E9;font-family:&quot;\">\n\tES6 Proxy\n<\/p>\n<p>\n\tproxy\u8005\uff0c\u4ee3\u7406\u4e5f\u3002\u6211\u7684\u7406\u89e3\uff0c\u5c31\u662f\u52a0\u58f3\uff0c\u5c31\u662f\u7eff\u6797\u3001\u5c31\u662f\u6536\u8d39\u7ad9\u3002\n<\/p>\n<p>\n\t\u5047\u8bbe\u4e00\u4e2a\u51fd\u6570\uff0c\u597d\u6bd4\u4e00\u6761\u9ad8\u901f\u516c\u8def\uff0c\u53ef\u4ee5\u4eceA\u5730\u5230B\u5730\u3002\u5177\u5907\u7684\u529f\u80fd\u5c31\u662f\u8054\u901a\u3002\u5177\u5907\u7684\u5c5e\u6027\u5c31\u662f\u540d\u5b57\u3001\u957f\u5ea6\u3001\u51e0\u8f66\u9053&nbsp; \u7b49\u3002\u5982\u679c\u6ca1\u6709\u4ee3\u7406\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u505a\uff0c\u90a3\u4e48\u4f60\u4e0d\u4f1a\u5bf9\u4ece\u4ed6\u4e0a\u9762&nbsp;\u901a\u8fc7\u7684\u90a3\u4e9b\u5ddd\u6d41\u4e0d\u606f\u7684\u8f66\u6709\u4efb\u4f55\u5f71\u54cd\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u4ee3\u7406\uff08\u6536\u8d39\u7ad9\uff09\u4ecb\u5165\uff0c\u5c06\u4ea7\u751f\u5982\u4e0b\u53d8\u5316\uff1a\n<\/p>\n<h3>\n\t1.\u57fa\u7840\u7bc7<br \/>\n<\/h3>\n<p>\n\t\u901a\u8fc7\u6536\u8d39\u7ad9\uff0c\u624d\u80fd\u4e0a\u8fd9\u6761\u516c\u8def\uff0c\u6536\u8d39\u7ad9\u540c\u65f6\u8bb0\u5f55\u4e86\u4f60\u4eceA\u5230B,\u8fd8\u662f\u4eceB\u5230A&nbsp;\u7684\u4fe1\u606f\u3002\n<\/p>\n<p>\n\t\u5b9a\u4e49\u8fd9\u6761\u9ad8\u901f\u516c\u8def\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">const road={name:\"AB\u9ad8\u901f\",width:8,length:300, direction:function(x,y){return x+\"-&gt;\"+y}}<\/pre>\n<p>\u5efa\u7acb\u4ee3\u7406<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">const roadhandler = { get(road, key, proxy) { const today = new Date(); console.log(`\u4f60\u8bbf\u95ee\u4e86\u5c5e\u6027 ${key} \u5728 ${today}`); return Reflect.get(road, key, proxy); } };&nbsp;\r\nconst roadproxy = new Proxy(road, roadhandler);\r\n\r\n<\/pre>\n<p>\n\t\n<\/p>\n<p>\n\t\u8bd5\u6267\u884c\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">roadproxy.name\r\nVM263:1 \u4f60\u8bbf\u95ee\u4e86\u5c5e\u6027 name \u5728 Thu Nov 01 2018 10:48:09 GMT+0800 (\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4)\r\n\"AB\u9ad8\u901f\"<\/pre>\n<pre class=\"prettyprint lang-js\">roadproxy.direction(\"a\",\"b\")\r\nVM263:1 \u4f60\u8bbf\u95ee\u4e86\u5c5e\u6027 direction \u5728 Thu Nov 01 2018 10:53:47 GMT+0800 (\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4)\r\n\"a-&gt;b\"<\/pre>\n<p>\n\troadhandler&nbsp; \u5c31\u662f\u90a3\u4e2a\u6536\u8d39\u7ad9\uff0c\u8981\u60f3\u7528\u8fd9\u6761\u8def\uff0c\u5c31\u5f97\u5148\u7ecf\u8fc7\u4ed6\uff0c\u7136\u540e\u624d\u80fd\u4e0a\u3002\n<\/p>\n<p>\n\t\u4f8b\u5b50\u4e00\u5c31\u662f\u901a\u8fc7\u4ed6\u770b\u4e86\u770b\uff0c\u8fd9\u6761\u8def\u662f\u4e0d\u662f\u4f60\u60f3\u8981\u8d70\u7684\n<\/p>\n<p>\n\t\u4f8b\u5b50\u4e8c\u5c31\u662f\u4e0a\u53bb\u8d70\uff0c\u4f60\u8981\u4ecea\u8d70\u5230b.\n<\/p>\n<p>\n\t\n<\/p>\n<h3>\n\t2.\u62bd\u79bb\u6821\u9a8c\u6a21\u5757<br \/>\n<\/h3>\n<p>\n\t\u6211\u7533\u8bf7\u4e86\u4e00\u7b14\u7ecf\u8d39\uff0c\u8981\u628a\u8fd9\u6761\u8def\u7684\u5bbd\u5ea6\u6539\u4e00\u4e0b\uff0c\u6b64\u65f6\u5404\u5bb6\u5c31\u8981\u4e0a\u62a5\uff0c\u6539\u6210\u7684\u5177\u4f53\u5bbd\u662f\u591a\u5c11\uff0c\u4f46\u4e5f\u6709\u4e0d\u6309\u7ea6\u5b9a\u62a5\u7684\uff0c\u6211\u4eec\u4e3a\u4e86\u9632\u6b62\u8bef\u62a5\u4e0a\u8981\u6821\u9a8c\u3002\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">let numericDataStore = { width: 8 }; numericDataStore = new Proxy(numericDataStore, { set(target, key, value, proxy) { if (typeof value !== 'number') { throw Error(\"\u53ea\u80fd\u4e0a\u62a5\u6570\u5b57\u7c7b\u578b\uff0c\u522b\u7684\u4e0d\u63a5\u53d7\u3002\"); } return Reflect.set(target, key, value, proxy); } });<\/pre>\n<p>\u8bd5\u7528\u4e00\u4e0b\uff1a<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">numericDataStore.width=\"1\"\r\nVM314:1 Uncaught Error: \u53ea\u80fd\u4e0a\u62a5\u6570\u5b57\u7c7b\u578b\uff0c\u522b\u7684\u4e0d\u63a5\u53d7\u3002\r\n    at Object.set (&lt;anonymous&gt;:1:159)\r\n    at &lt;anonymous&gt;:1:23\r\nset @ VM314:1\r\n(anonymous) @ VM336:1<\/pre>\n<pre class=\"prettyprint lang-js\">numericDataStore.width=12\r\n12<\/pre>\n<p>\n\t\n<\/p>\n<p>\n\t\u66f4\u590d\u6742\u7684\u6821\u9a8c\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">function createValidator(target, validator) {\r\n&nbsp; &nbsp; return new Proxy(target, {\r\n&nbsp; &nbsp; &nbsp; &nbsp; _validator: validator,\r\n&nbsp; &nbsp; &nbsp; &nbsp; set(target, key, value, proxy) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (target.hasOwnProperty(key)) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let validator = this._validator[key];\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( !! validator(value)) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Reflect.set(target, key, value, proxy);\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw Error(`Cannot set $ {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to $ {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }.Invalid.`);\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw Error(`$ {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is not a valid property`)\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; });\r\n}\r\nconst personValidators = {\r\n&nbsp; &nbsp; name(val) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; return typeof val === 'string';\r\n&nbsp; &nbsp; },\r\n&nbsp; &nbsp; age(val) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; return typeof age === 'number' &amp;&amp; age &gt; 18;\r\n&nbsp; &nbsp; }\r\n}\r\nclass Person {\r\n&nbsp; &nbsp; constructor(name, age) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;\r\n&nbsp; &nbsp; &nbsp; &nbsp; this.age = age;\r\n&nbsp; &nbsp; &nbsp; &nbsp; return createValidator(this, personValidators);\r\n&nbsp; &nbsp; }\r\n}\r\nconst bill = new Person('Bill', 25);\r\n\r\n<\/pre>\n<h3>\n\t3.<span style=\"color:#00A3CF;font-family:&quot;background-color:#FFFFFF;font-size:1.6em;\">\u79c1\u6709\u5c5e\u6027&nbsp; \u4f7f\u5bf9\u8c61\u7684\u79c1\u6709\u5c5e\u6027\u4e0d\u80fd\u88ab\u5916\u90e8\u8bbf\u95ee\u5230<\/span><br \/>\n<\/h3>\n<p>\n\t\n<\/p>\n<p>\n\t<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\u5728 JavaScript \u6216\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u5927\u5bb6\u4f1a\u7ea6\u5b9a\u4fd7\u6210\u5730\u5728\u53d8\u91cf\u540d\u4e4b\u524d\u6dfb\u52a0\u4e0b\u5212\u7ebf&nbsp;<\/span>_<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u6765\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u79c1\u6709\u5c5e\u6027\uff08\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u79c1\u6709\uff09\uff0c\u4f46\u6211\u4eec\u65e0\u6cd5\u4fdd\u8bc1\u771f\u7684\u6ca1\u4eba\u4f1a\u53bb\u8bbf\u95ee\u6216\u4fee\u6539\u5b83\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u79c1\u6709\u7684&nbsp;<\/span>apiKey<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\uff0c\u4fbf\u4e8e&nbsp;<\/span>api<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u8fd9\u4e2a\u5bf9\u8c61\u5185\u90e8\u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u4f46\u4e0d\u5e0c\u671b\u4ece\u5916\u90e8\u4e5f\u80fd\u591f\u8bbf\u95ee&nbsp;<\/span>api._apiKey<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">:<\/span>\n<\/p>\n<p>\n\t<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\"><br \/>\n<\/span>\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">var api = {\r\n    _apiKey: '123abc456def',\r\n    \/* mock methods that use this._apiKey *\/\r\n    getUsers: function() {},\r\n    getUser: function(userId) {},\r\n    setUser: function(userId, config) {}\r\n}; \/\/ logs '123abc456def'; console.log(\"An apiKey we want to keep private\", api._apiKey); \/\/ get and mutate _apiKeys as desired \r\nvar apiKey = api._apiKey; \r\napi._apiKey = '987654321';\r\n<\/pre>\n<p><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\u5f88\u663e\u7136\uff0c\u7ea6\u5b9a\u4fd7\u6210\u662f\u6ca1\u6709\u675f\u7f1a\u529b\u7684\u3002\u4f7f\u7528 ES6 Proxy \u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0\u771f\u5b9e\u7684\u79c1\u6709\u53d8\u91cf\u4e86\uff0c\u4e0b\u9762\u9488\u5bf9\u4e0d\u540c\u7684\u8bfb\u53d6\u65b9\u5f0f\u6f14\u793a\u4e24\u4e2a\u4e0d\u540c\u7684\u79c1\u6709\u5316\u65b9\u6cd5\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 set \/ get \u62e6\u622a\u8bfb\u5199\u8bf7\u6c42\u5e76\u8fd4\u56de&nbsp;<\/span>undefined<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">:<\/span> <\/p>\n<p>\n\t\n<\/p>\n<p>\n\t<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\"> <\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">let api = {\r\n    _apiKey: '123abc456def',\r\n    getUsers: function() {},\r\n    getUser: function(userId) {},\r\n    setUser: function(userId, config) {}\r\n};\r\nconst RESTRICTED = ['_apiKey'];\r\napi = new Proxy(api, {\r\n    get(target, key, proxy) {\r\n        if (RESTRICTED.indexOf(key) &gt; -1) {\r\n            throw Error(`$ {\r\n                key\r\n            }\r\n            is restricted.Please see api documentation\r\n            for further info.`);\r\n        }\r\n        return Reflect.get(target, key, proxy);\r\n    },\r\n    set(target, key, value, proxy) {\r\n        if (RESTRICTED.indexOf(key) &gt; -1) {\r\n            throw Error(`$ {\r\n                key\r\n            }\r\n            is restricted.Please see api documentation\r\n            for further info.`);\r\n        }\r\n        return Reflect.get(target, key, value, proxy);\r\n    }\r\n}); \/\/ \u4ee5\u4e0b\u64cd\u4f5c\u90fd\u4f1a\u629b\u51fa\u9519\u8bef\r\n console.log(api._apiKey); \r\napi._apiKey = '987654321'; \r\n<\/pre>\n<pre class=\"prettyprint lang-js\">var api = {\r\n    _apiKey: '123abc456def',\r\n    getUsers: function() {},\r\n    getUser: function(userId) {},\r\n    setUser: function(userId, config) {}\r\n};\r\nconst RESTRICTED = ['_apiKey'];\r\napi = new Proxy(api, {\r\n    has(target, key) {\r\n        return (RESTRICTED.indexOf(key) &gt; -1) ? false: Reflect.has(target, key);\r\n    }\r\n}); \/\/ these log false, and `for in` iterators will ignore _apiKey \r\nconsole.log(\"_apiKey\" in api);\r\nfor (var key in api) {\r\n    if (api.hasOwnProperty(key) &amp;&amp; key === \"_apiKey\") {\r\n        console.log(\"This will never be logged because the proxy obscures _apiKey...\")\r\n    }\r\n}<\/pre>\n<h3>\n\t4.<span style=\"color:#00A3CF;font-family:&quot;background-color:#FFFFFF;font-size:1.6em;\">\u8bbf\u95ee\u65e5\u5fd7&nbsp; \u8fd9\u4e2a\u5f88\u597d\u7406\u89e3\uff0c\u5c31\u662f\u8bb0\u5f55\u4e00\u4e0b\u4f60\u8bbf\u95ee\u4e86\u5bf9\u8c61\u7684\u54ea\u4e9b\u5c5e\u6027<\/span><br \/>\n<\/h3>\n<p>\n\t<span><span style=\"font-size:19.2px;\"><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\u5bf9\u4e8e\u90a3\u4e9b\u8c03\u7528\u9891\u7e41\u3001\u8fd0\u884c\u7f13\u6162\u6216\u5360\u7528\u6267\u884c\u73af\u5883\u8d44\u6e90\u8f83\u591a\u7684\u5c5e\u6027\u6216\u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u4f1a\u5e0c\u671b\u8bb0\u5f55\u5b83\u4eec\u7684\u4f7f\u7528\u60c5\u51b5\u6216\u6027\u80fd\u8868\u73b0\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528 Proxy \u5145\u5f53\u4e2d\u95f4\u4ef6\u7684\u89d2\u8272\uff0c\u8f7b\u800c\u6613\u4e3e\u5b9e\u73b0\u65e5\u5fd7\u529f\u80fd\uff1a<\/span><\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:19.2px;\"><span style=\"background-color:#EEEEEE;\"> <\/span><\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">let api = {\r\n    _apiKey: '123abc456def',\r\n    getUsers: function() {\r\n        \/* ... *\/\r\n    },\r\n    getUser: function(userId) {\r\n        \/* ... *\/\r\n    },\r\n    setUser: function(userId, config) {\r\n        \/* ... *\/\r\n    }\r\n};\r\nfunction logMethodAsync(timestamp, method) {\r\n    setTimeout(function() {\r\n        console.log(`$ {\r\n            timestamp\r\n        } - Logging $ {\r\n            method\r\n        }\r\n        request asynchronously.`);\r\n    },\r\n    0)\r\n}\r\napi = new Proxy(api, {\r\n    get: function(target, key, proxy) {\r\n        var value = target[key];\r\n        return function(...arguments) {\r\n            logMethodAsync(new Date(), key);\r\n            return Reflect.apply(value, target, arguments);\r\n        };\r\n    }\r\n});\r\napi.getUsers();<\/pre>\n<h3>\n\t5.<span style=\"color:#00A3CF;font-family:&quot;background-color:#FFFFFF;font-size:1.6em;\">\u9884\u8b66\u548c\u62e6\u622a<\/span><br \/>\n<\/h3>\n<p>\n\t<span style=\"color:#00A3CF;font-family:&quot;background-color:#FFFFFF;font-size:1.6em;\"><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\u5047\u8bbe\u4f60\u4e0d\u60f3\u8ba9\u5176\u4ed6\u5f00\u53d1\u8005\u5220\u9664&nbsp;<\/span>noDelete<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u5c5e\u6027\uff0c\u8fd8\u60f3\u8ba9\u8c03\u7528&nbsp;<\/span>oldMethod<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u7684\u5f00\u53d1\u8005\u4e86\u89e3\u5230\u8fd9\u4e2a\u65b9\u6cd5\u5df2\u7ecf\u88ab\u5e9f\u5f03\u4e86\uff0c\u6216\u8005\u544a\u8bc9\u5f00\u53d1\u8005\u4e0d\u8981\u4fee\u6539&nbsp;<\/span>doNotChange<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u5c5e\u6027\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u4f7f\u7528 Proxy \u6765\u5b9e\u73b0\uff1a<\/span><br \/>\n<\/span>\n<\/p>\n<p>\n\t<span style=\"background-color:#EEEEEE;\"><\/span>\n<\/p>\n<p>\n\t<span style=\"background-color:#EEEEEE;\"> <\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">let dataStore = {\r\n    noDelete: 1235,\r\n    oldMethod: function() {\r\n        \/*...*\/\r\n    },\r\n    doNotChange: \"tried and true\"\r\n};\r\nconst NODELETE = ['noDelete'];\r\nconst NOCHANGE = ['doNotChange'];\r\nconst DEPRECATED = ['oldMethod'];\r\ndataStore = new Proxy(dataStore, {\r\n    set(target, key, value, proxy) {\r\n        if (NOCHANGE.includes(key)) {\r\n            throw Error(`Error ! $ {\r\n                key\r\n            }\r\n            is immutable.`);\r\n        }\r\n        return Reflect.set(target, key, value, proxy);\r\n    },\r\n    deleteProperty(target, key) {\r\n        if (NODELETE.includes(key)) {\r\n            throw Error(`Error ! $ {\r\n                key\r\n            }\r\n            cannot be deleted.`);\r\n        }\r\n        return Reflect.deleteProperty(target, key);\r\n    },\r\n    get(target, key, proxy) {\r\n        if (DEPRECATED.includes(key)) {\r\n            console.warn(`Warning ! $ {\r\n                key\r\n            }\r\n            is deprecated.`);\r\n        }\r\n        var val = target[key];\r\n        return typeof val === 'function' ?\r\n        function(...args) {\r\n            Reflect.apply(target[key], target, args);\r\n        }: val;\r\n    }\r\n}); \/\/ these will throw errors or log warnings, respectively \r\ndataStore.doNotChange = \"foo\"; \r\ndelete dataStore.noDelete; \r\ndataStore.oldMethod();\r\n<\/pre>\n<h3 style=\"font-size:1.6em;color:#00A3CF;font-family:&quot;background-color:#FFFFFF;\">\n\t6. \u8fc7\u6ee4\u64cd\u4f5c<br \/>\n<\/h3>\n<p><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">\u67d0\u4e9b\u64cd\u4f5c\u4f1a\u975e\u5e38\u5360\u7528\u8d44\u6e90\uff0c\u6bd4\u5982\u4f20\u8f93\u5927\u6587\u4ef6\uff0c\u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5728\u5206\u5757\u53d1\u9001\u4e86\uff0c\u5c31\u4e0d\u9700\u8981\u5728\u5bf9\u65b0\u7684\u8bf7\u6c42\u4f5c\u51fa\u76f8\u5e94\uff08\u975e\u7edd\u5bf9\uff09\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528 Proxy \u5bf9\u5f53\u8bf7\u6c42\u8fdb\u884c\u7279\u5f81\u68c0\u6d4b\uff0c\u5e76\u6839\u636e\u7279\u5f81\u8fc7\u6ee4\u51fa\u54ea\u4e9b\u662f\u4e0d\u9700\u8981\u54cd\u5e94\u7684\uff0c\u54ea\u4e9b\u662f\u9700\u8981\u54cd\u5e94\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u7b80\u5355\u6f14\u793a\u4e86\u8fc7\u6ee4\u7279\u5f81\u7684\u65b9\u5f0f\uff0c\u5e76\u4e0d\u662f\u5b8c\u6574\u4ee3\u7801\uff0c\u76f8\u4fe1\u5927\u5bb6\u4f1a\u7406\u89e3\u5176\u4e2d\u7684\u5999\u5904\uff1a<\/span> <\/p>\n<p>\n\t\n<\/p>\n<p>\n\t<span style=\"background-color:#EEEEEE;\"><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\"> <\/span><\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">let obj = {\r\n    getGiantFile: function(fileId) {\r\n        \/*...*\/\r\n    }\r\n};\r\nobj = new Proxy(obj, {\r\n    get(target, key, proxy) {\r\n        return function(...args) {\r\n            const id = args[0];\r\n            let isEnroute = checkEnroute(id);\r\n            let isDownloading = checkStatus(id);\r\n            let cached = getCached(id);\r\n            if (isEnroute || isDownloading) {\r\n                return false;\r\n            }\r\n            if (cached) {\r\n                return cached;\r\n            }\r\n            return Reflect.apply(target[key], target, args);\r\n        }\r\n    }\r\n});<\/pre>\n<h3>\n\t7.<span style=\"color:#00A3CF;font-family:&quot;background-color:#EEEEEE;font-size:1.6em;\">\u4e2d\u65ad\u4ee3\u7406<\/span><br \/>\n<\/h3>\n<p>\n\t\n<\/p>\n<p>\n\t<span style=\"background-color:#EEEEEE;\"><span><span style=\"font-size:19.2px;\"><span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">Proxy \u652f\u6301\u968f\u65f6\u53d6\u6d88\u5bf9&nbsp;<\/span>target<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u7684\u4ee3\u7406\uff0c\u8fd9\u4e00\u64cd\u4f5c\u5e38\u7528\u4e8e\u5b8c\u5168\u5c01\u95ed\u5bf9\u6570\u636e\u6216\u63a5\u53e3\u7684\u8bbf\u95ee\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86&nbsp;<\/span>Proxy.revocable<span style=\"color:#404040;font-family:&quot;font-size:14px;background-color:#EEEEEE;\">&nbsp;\u65b9\u6cd5\u521b\u5efa\u4e86\u53ef\u64a4\u9500\u4ee3\u7406\u7684\u4ee3\u7406\u5bf9\u8c61\uff1a<\/span><\/span><\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"background-color:#EEEEEE;\"><span style=\"font-size:19.2px;\"><span style=\"background-color:#EEEEEE;\"> <\/span><\/span><\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">let sensitiveData = {\r\n    username: 'devbryce'\r\n};\r\nconst {\r\n    sensitiveData,\r\n    revokeAccess\r\n} = Proxy.revocable(sensitiveData, handler);\r\nfunction handleSuspectedHack() {\r\n    revokeAccess();\r\n} \/\/ logs 'devbryce' console.log(sensitiveData.username);\r\n handleSuspectedHack(); \/\/ TypeError: Revoked \r\nconsole.log(sensitiveData.username);\r\n<\/pre>\n<p>\n<span style=\"background-color:#EEEEEE;\"><\/span> <\/p>\n<p>\n\t\n<\/p>\n<p>\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ES6 Proxy proxy\u8005\uff0c\u4ee3\u7406\u4e5f\u3002\u6211\u7684\u7406\u89e3\uff0c\u5c31\u662f\u52a0\u58f3\uff0c\u5c31\u662f\u7eff\u6797\u3001\u5c31\u662f\u6536\u8d39\u7ad9\u3002 \u5047\u8bbe\u4e00\u4e2a\u51fd\u6570\uff0c\u597d\u6bd4\u4e00\u6761\u9ad8&#8230;<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[38],"tags":[],"_links":{"self":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3138"}],"collection":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/comments?post=3138"}],"version-history":[{"count":7,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3138\/revisions"}],"predecessor-version":[{"id":3145,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3138\/revisions\/3145"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media?parent=3138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/categories?post=3138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/tags?post=3138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}