mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-14 02:56:56 +08:00
157 lines
9.2 KiB
HTML
157 lines
9.2 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p><strong><code>handler.defineProperty()</code></strong> 用于拦截对对象的 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 操作。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript">var p = new Proxy(target, {
|
||
defineProperty: function(target, property, descriptor) {
|
||
}
|
||
});
|
||
</code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<p>下列参数将会被传递给 <code>defineProperty</code> 方法。<code> this</code> 绑定在 handler 对象上。</p>
|
||
<dl>
|
||
<dt><code>target</code></dt>
|
||
<dd>目标对象。</dd>
|
||
<dt><code>property</code></dt>
|
||
<dd>待检索其描述的属性名。</dd>
|
||
<dt><code>descriptor</code></dt>
|
||
<dd>待定义或修改的属性的描述符。</dd>
|
||
</dl>
|
||
<h3 id="返回值">返回值</h3>
|
||
<p><code>defineProperty</code> 方法必须以一个 <a href="Reference/Boolean" title="此页面仍未被本地化, 期待您的翻译!"><code>Boolean</code></a> 返回,表示定义该属性的操作成功与否。</p>
|
||
<h2 id="描述">描述</h2>
|
||
<p><code><strong>handler.defineProperty()</strong></code> 用于拦截对对象的 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 操作。</p>
|
||
<h3 id="拦截">拦截</h3>
|
||
<p>该方法会拦截目标对象的以下操作 :</p>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Reflect/defineProperty" title="静态方法 Reflect.defineProperty() 基本等同于 Object.defineProperty() 方法,唯一不同是返回 Boolean 值。"><code>Reflect.defineProperty()</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>不能添加或者修改一个属性为不可配置的,如果它不作为一个目标对象的不可配置的属性存在的话。</li>
|
||
<li>如果目标对象存在一个对应的可配置属性,这个属性可能不会是不可配置的。</li>
|
||
<li>如果一个属性在目标对象中存在对应的属性,那么 <code>Object.defineProperty(target, prop, descriptor)</code> 将不会抛出异常。</li>
|
||
<li>在严格模式下, <code>false</code> 作为<code> handler.defineProperty</code> 方法的返回值的话将会抛出 <a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 异常.</li>
|
||
</ul>
|
||
<h2 id="示例">示例</h2>
|
||
<p>以下代码演示如何拦截对目标对象的 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 操作。</p>
|
||
<pre><code class="language-javascript">var p = new Proxy({}, {
|
||
defineProperty: function(target, prop, descriptor) {
|
||
console.log('called: ' + prop);
|
||
return true;
|
||
}
|
||
});
|
||
|
||
var desc = { configurable: true, enumerable: true, value: 10 };
|
||
Object.defineProperty(p, 'a', desc); // "called: a"
|
||
</code></pre>
|
||
<p>当调用 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 或者 <a href="Reference/Global_Objects/Reflect/defineProperty" title="静态方法 Reflect.defineProperty() 基本等同于 Object.defineProperty() 方法,唯一不同是返回 Boolean 值。"><code>Reflect.defineProperty()</code></a>,传递给 <code>defineProperty</code> 的 <code>descriptor</code> 有一个限制 - 只有以下属性才有用,非标准的属性将会被无视 :</p>
|
||
<ul>
|
||
<li><code>enumerable</code></li>
|
||
<li><code>configurable</code></li>
|
||
<li><code>writable</code></li>
|
||
<li><code>value</code></li>
|
||
<li><code>get</code></li>
|
||
<li><code>set</code></li>
|
||
</ul>
|
||
<pre><code class="language-javascript">var p = new Proxy({}, {
|
||
defineProperty(target, prop, descriptor) {
|
||
console.log(descriptor);
|
||
return Reflect.defineProperty(target, prop, descriptor);
|
||
}
|
||
});
|
||
|
||
Object.defineProperty(p, 'name', {
|
||
value: 'proxy',
|
||
type: 'custom'
|
||
}); // { value: 'proxy' }
|
||
</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-defineownproperty-p-desc" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">[[DefineOwnProperty]]</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-defineownproperty-p-desc" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">[[DefineOwnProperty]]</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/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Reflect/defineProperty" title="静态方法 Reflect.defineProperty() 基本等同于 Object.defineProperty() 方法,唯一不同是返回 Boolean 值。"><code>Reflect.defineProperty()</code></a></li>
|
||
</ul>
|
||
</article> |