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

136 lines
6.8 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>
<p><code><strong>handler.set()</strong></code> 方法用于拦截设置属性值的操作</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">var p = new Proxy(target, {
set: function(target, property, value, receiver) {
}
});
</code></pre>
<h3 id="参数">参数</h3>
<p>以下是传递给set方法的参数<code>this上下文绑定在</code>handler对象上.</p>
<dl>
<dt><code>target</code></dt>
<dd>目标对象。</dd>
<dt><code>property</code></dt>
<dd>被设置的属性名。</dd>
<dt><code>value</code></dt>
<dd>被设置的新值。</dd>
<dt><code>receiver</code></dt>
<dd>最初被调用的对象。通常是proxy本身但handler的set方法也有可能在原型链上或以其他方式被间接地调用因此不一定是proxy本身</dd>
<dd>比如,假设有一段代码执行 <code>obj.name = "jen"obj不是一个proxy且自身不含name属性但它的原型链上有一个proxy那么那个proxy的set拦截函数会被调用此时obj会作为receiver参数传进来。</code></dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>set方法应该返回一个布尔值返回true代表此次设置属性成功了如果返回false且设置属性操作发生在严格模式下那么会抛出一个<code>TypeError</code></p>
<h2 id="描述">描述</h2>
<p><code><strong>handler.set</strong></code> 方法用于拦截设置属性值的操作。</p>
<h3 id="拦截">拦截</h3>
<p>该方法会拦截目标对象的以下操作:</p>
<ul>
<li>指定属性值: <code>proxy[foo] = bar</code> 和 <code>proxy.foo = bar</code></li>
<li>指定继承者的属性值: <code>Object.create(proxy)[foo] = bar</code></li>
<li><a href="Reference/Global_Objects/Reflect/set" title="静态方法 Reflect.set() 工作方式就像在一个对象上设置一个属性。"><code>Reflect.set()</code></a></li>
</ul>
<h3 id="约束">约束</h3>
<p>如果违背以下的约束条件proxy会抛出一个<a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>:</p>
<ul>
<li>若目标属性是不可写及不可配置的,则不能改变它的值。</li>
<li>如果目标属性没有配置存储方法即set方法是undefined的则不能设置它的值。</li>
<li>在严格模式下若set方法返回false则会抛出一个 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 异常。</li>
</ul>
<h2 id="示例">示例</h2>
<p>以下代码演示如何捕获属性的设置操作。</p>
<pre><code class="language-javascript">var p = new Proxy({}, {
set: function(target, prop, value, receiver) {
console.log("called: " + prop + " = " + value);
return true;
}
});
p.a = 10; // "called: a = 10"
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</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-set-p-v-receiver" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">[[Set]]</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">[[Set]]</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><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></div>
<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: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><a href="/en-US/Firefox/Releases/18" title="Released on 2013-01-08.">18</a> (18)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</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: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>18.0 (18)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="另见">另见</h2>
<ul>
<li><a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。"><code>Proxy</code></a></li>
<li><a href="Reference/Global_Objects/Proxy/handler" title="处理器对象用来自定义代理对象的各种可代理操作。"><code>handler</code></a></li>
<li><a href="Reference/Global_Objects/Reflect/set" title="静态方法 Reflect.set() 工作方式就像在一个对象上设置一个属性。"><code>Reflect.set()</code></a></li>
</ul>
</article>