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

118 lines
5.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="Summary" name="Summary">概述</h2>
<p><code><strong>Proxy.revocable()</strong></code> 方法可以用来创建一个可撤销的代理对象。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript">Proxy.revocable(target, handler);
</code></pre>
<p></p><dl>
<dt><code>target</code></dt>
<dd><code>Proxy</code>包装的目标对象(可以是任何类型的对象,包括原生数组,函数,甚至另一个代理)。</dd>
<dt><code>handler</code></dt>
<dd>一个对象,其属性是当执行一个操作时定义代理的行为的函数。</dd>
</dl><p></p>
<h3 id="Return_value" name="Return_value">返回值</h3>
<p>返回一个包含了所生成的代理对象本身以及该代理对象的撤销方法的对象。</p>
<h2 id="描述">描述</h2>
<p>该方法的返回值是一个对象,其结构为: <code>{"proxy": proxy, "revoke": revoke},其中:</code></p>
<dl>
<dt><code>proxy</code></dt>
<dd>表示新生成的代理对象本身,和用一般方式 <code>new Proxy(target, handler)</code> 创建的代理对象没什么不同,只是它可以被撤销掉。</dd>
<dt><code>revoke</code></dt>
<dd>撤销方法,调用的时候不需要加任何参数,就可以撤销掉和它一起生成的那个代理对象。</dd>
</dl>
<p>一旦某个代理对象被撤销,它将变的几乎完全不可用,在它身上执行任何的<strong>可代理操作</strong>都会抛出 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 异常(注意,可代理操作一共有 <a href="Reference/Global_Objects/Proxy#Methods_of_the_handler_object" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。"><code>14 种</code></a>,执行这 14 种操作以外的操作不会抛出异常)。一旦被撤销,这个代理对象永远不可能恢复到原来的状态,同时和它关联的<strong>目标对象</strong>以及<strong>处理器对象</strong>将有可能被垃圾回收掉。调用撤销方法多次将不会有任何效果,当然,也不会报错。</p>
<h2 id="Examples" name="Examples">示例</h2>
<pre><code class="language-javascript">var revocable = Proxy.revocable({}, {
get(target, name) {
return "[[" + name + "]]";
}
});
var proxy = revocable.proxy;
proxy.foo; // "[[foo]]"
revocable.revoke(); // 执行撤销方法
proxy.foo; // TypeError
proxy.foo = 1 // 同样 TypeError
delete proxy.foo; // 还是 TypeError
typeof proxy // "object",因为 typeof 不属于可代理操作
</code></pre>
<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.revocable" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Proxy Revocation Functions</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/34" title="Released on 2014-12-01.">34</a> (34)</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>
<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/34" title="Released on 2014-12-01.">34</a> (34)</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="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。"><code>Proxy</code></a></li>
</ul>
</article>