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

144 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> <strong><code>handler.has()</code></strong> 方法可以看作是针对 <a href="Reference/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a> 操作的钩子.</p>
<h2 id="Syntax">Syntax</h2>
<pre><code class="language-javascript">var p = new Proxy(target, {
has: function(target, prop) {
}
});
</code></pre>
<h3 id="Parameters">Parameters</h3>
<p><code>下面是传递给 has</code> 方法的参数. <code>this</code> is bound to the handler.</p>
<dl>
<dt><code>target</code></dt>
<dd>目标对象.</dd>
<dt><code>prop</code></dt>
<dd>需要检查是否存在的属性.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p><code>has</code> 方法返回一个 boolean 属性的值.</p>
<h2 id="Description">Description</h2>
<p><code><strong>handler.has</strong></code> 方法可以看作是针对 <a href="Reference/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a> 操作的钩子.</p>
<h3 id="Interceptions">Interceptions</h3>
<p>这个钩子可以拦截下面这些操作:</p>
<ul>
<li>属性查询: <code>foo in proxy</code></li>
<li>继承属性查询: <code>foo in Object.create(proxy)</code></li>
<li><code>with</code> 检查<code>: with(proxy) { (foo); }</code></li>
<li><a href="Reference/Global_Objects/Reflect/has" title="静态方法 Reflect.has() 作用与 in 操作符 相同。"><code>Reflect.has()</code></a></li>
</ul>
<h3 id="Invariants">Invariants</h3>
<p>如果违反了下面这些规则,  proxy 将会抛出 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>:</p>
<ul>
<li>如果目标对象的某一属性本身不可被配置,则该属性不能够被代理隐藏.</li>
<li>如果目标对象为不可扩展对象,则该对象的属性不能够被代理隐藏</li>
</ul>
<h2 id="Examples">Examples</h2>
<p>下面的代码 hook 了 <a href="Reference/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a> 操作.</p>
<pre><code class="language-javascript">var p = new Proxy({}, {
has: function(target, prop) {
console.log('called: ' + prop);
return true;
}
});
console.log('a' in p); // "called: a"
// true
</code></pre>
<p>下面的代码违反了规则.</p>
<pre><code class="language-javascript">var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
has: function(target, prop) {
return false;
}
});
'a' in p; // TypeError is thrown
</code></pre>
<h2 id="Specifications">Specifications</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-hasproperty-p" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">[[HasProperty]]</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-hasproperty-p" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">[[HasProperty]]</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</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="See_also">See also</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/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a> operator</li>
<li><a href="Reference/Global_Objects/Reflect/has" title="静态方法 Reflect.has() 作用与 in 操作符 相同。"><code>Reflect.has()</code></a></li>
</ul>
</article>