2019-04-21 11:50:48 +08:00

112 lines
9.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="概述">概述</h2>
<p>处理器对象用来自定义<a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。">代理对象</a>的各种可代理操作。</p>
<h2 id="方法">方法</h2>
<p>一共有 13 种可代理操作每种操作的代号属性名/方法名)和触发这种操作的方式列举如下。注意,如果没有定义某种操作,那么这种操作会被转发到目标对象身上。</p>
<dl>
<dt><a href="Reference/Global_Objects/Proxy/handler/getPrototypeOf" title="handler.getPrototypeOf() 是一个代理方法,当读取代理对象的原型时,该方法就会被调用。"><code>handler.getPrototypeOf()</code></a></dt>
<dd>在读取代理对象的原型时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/getPrototypeOf" title="Object.getPrototypeOf() 方法返回指定对象的原型(内部[[Prototype]]属性的值)。"><code>Object.getPrototypeOf</code></a>(proxy)</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/setPrototypeOf" title="handler.setPrototypeOf() 方法主要用来拦截 Object.setPrototypeOf()."><code>handler.setPrototypeOf()</code></a></dt>
<dd>在设置代理对象的原型时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/setPrototypeOf" title="如果对象的[[Prototype]]被修改成不可扩展(通过 Object.isExtensible()查看)就会抛出 TypeError异常。如果prototype参数不是一个对象或者null(例如数字字符串boolean或者 undefined)则什么都不做。否则该方法将obj的[[Prototype]]修改为新的值。"><code>Object.setPrototypeOf</code></a>(proxy, null)</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/isExtensible" title="下列参数将会被传递给 isExtensible方法。 this 绑定在 handler 对象上。"><code>handler.isExtensible()</code></a></dt>
<dd>在判断一个代理对象是否是可扩展时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/isExtensible" title="Object.isExtensible() 方法判断一个对象是否是可扩展的(是否可以在它上面添加新的属性)。"><code>Object.isExtensible</code></a>(proxy)</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/preventExtensions" title="handler.preventExtensions() 方法用于设置对"><code>handler.preventExtensions()</code></a></dt>
<dd>在让一个代理对象不可扩展时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/preventExtensions" title="Object.preventExtensions()方法让一个对象变的不可扩展,也就是永远不能再添加新的属性。"><code>Object.preventExtensions</code></a>(proxy)</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor" title="handler.getOwnPropertyDescriptor() 方法是 Object.getOwnPropertyDescriptor()  的陷阱。"><code>handler.getOwnPropertyDescriptor()</code></a></dt>
<dd>在获取代理对象某个属性的属性描述时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/getOwnPropertyDescriptor" title="Object.getOwnPropertyDescriptor() 方法返回指定对象上一个自有属性对应的属性描述符。(自有属性指的是直接赋予该对象的属性,不需要从原型链上进行查找的属性)"><code>Object.getOwnPropertyDescriptor</code></a>(proxy, "foo")</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/defineProperty" title="handler.defineProperty() 用于拦截对对象的 Object.defineProperty() 操作。"><code>handler.defineProperty()</code></a></dt>
<dd>在定义代理对象某个属性时的属性描述时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty</code></a>(proxy, "foo", {})</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/has" title="handler.has() 方法可以看作是针对 in 操作的钩子."><code>handler.has()</code></a></dt>
<dd>在判断代理对象是否拥有某个属性时触发该操作,比如在执行 <code>"foo" <a href="Reference/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a> proxy</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/get" title="handler.get() 方法用于拦截对象的读取属性操作。"><code>handler.get()</code></a></dt>
<dd>在读取代理对象的某个属性时触发该操作,比如在执行 <code>proxy.foo</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/set" title="handler.set() 方法用于拦截设置属性值的操作"><code>handler.set()</code></a></dt>
<dd>在给代理对象的某个属性赋值时触发该操作,比如在执行 <code>proxy.foo = 1</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/deleteProperty" title="handler.deleteProperty() 方法用于拦截对对象属性的 delete 操作。"><code>handler.deleteProperty()</code></a></dt>
<dd>在删除代理对象的某个属性时触发该操作,比如在执行 <code>delete proxy.foo</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/ownKeys" title="handler.ownKeys() 方法用于拦截 Reflect.ownKeys()."><code>handler.ownKeys()</code></a></dt>
<dd>在获取代理对象的所有属性键时触发该操作,比如在执行 <code><a href="Reference/Global_Objects/Object/getOwnPropertyNames" title="Object.getOwnPropertyNames()方法返回一个由指定对象的所有自身属性的属性名包括不可枚举属性但不包括Symbol值作为名称的属性组成的数组。"><code>Object.getOwnPropertyNames</code></a>(proxy)</code> 时。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/apply" title="handler.apply() 方法用于拦截函数的调用。"><code>handler.apply()</code></a></dt>
<dd>当目标对象为函数,且被调用时触发。</dd>
<dt><a href="Reference/Global_Objects/Proxy/handler/construct" title="handler.construct() 方法用于拦截new 操作符. 为了使new操作符在生成的Proxy对象上生效用于初始化代理的目标对象自身必须具有[[Construct]]内部方法(即 new target 必须是有效的)。"><code>handler.construct()</code></a></dt>
<dd>在给一个目标对象为构造函数的代理对象构造实例时触发该操作,比如在执行<code>new proxy()</code> 时。</dd>
</dl>
<h2 id="Specifications" name="Specifications">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范名称</th>
<th scope="col">规范状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-proxy-object-internal-methods-and-internal-slots" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Proxy Object Internal Methods and Internal Slots</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><a href="/en-US/Firefox/Releases/18" title="Released on 2013-01-08.">18</a> (18)</td>
<td>12</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><a href="/en-US/Firefox/Releases/18" title="Released on 2013-01-08.">18</a> (18)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。"><code>Proxy</code></a></li>
</ul>
</article>