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

119 lines
6.9 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 class="blockIndicator deprecated deprecatedHeader">
<p><strong><span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span> 已废弃</strong><br/>该特性已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。</p>
</div></div>
<div><code><strong>__lookupSetter__</strong></code> 方法是用来返回一个对象的某个属性上绑定了 setter (设置器)的钩子函数的引用。</div>
<div> </div>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><var>obj</var>.__lookupSetter__(<var>sprop</var>)</code></code></pre>
<h3 id="参数说明">参数说明</h3>
<dl>
<dt><code>sprop</code></dt>
<dd>一个字符串类型,表示要返回的 setter 钩子的函数名。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>一个绑定了setter的特殊属性的函数引用。</p>
<h2 id="描述">描述</h2>
<p>如果一个 setter 被定义在了一个对象的属性上,则不能直接通过该属性来获取引用 setter 所设置的钩子的函数因为该属性是该函数的返回值__lookupSetter__ 可以被用来获取对 setter 函数的引用。</p>
<p>不过现在可以使用标准的方法:</p>
<p><a href="Reference/Global_Objects/Object/getOwnPropertyDescriptor" title="Object.getOwnPropertyDescriptor() 方法返回指定对象上一个自有属性对应的属性描述符。(自有属性指的是直接赋予该对象的属性,不需要从原型链上进行查找的属性)"><code>Object.getOwnPropertyDescriptor()</code></a>.</p>
<h2 id="示例">示例</h2>
<pre><code class="language-javascript">var obj = {
set foo(value) {
this.bar = value;
}
};
// 非标准,并且不推荐使用。
obj.__lookupSetter__('foo')
// (function(value) { this.bar = value; })
// 标准且推荐使用的方式。
Object.getOwnPropertyDescriptor(obj, 'foo').set;
// (function(value) { this.bar = value; })
</code></pre>
<h2 id="规范">规范</h2>
<table class="spectable standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-object.prototype.__lookupSetter__" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Object.prototype.__lookupSetter__()</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td>包含在规范性附件中用于Web浏览器的附加ECMAScript遗留功能请注意这份规范编撰的内容已经是准备实现的了</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: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>11</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</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: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</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>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/__lookupGetter__" title="__lookupGetter__ 方法会返回当前对象上指定属性的属性读取访问器函数getter。"><code>Object.prototype.__lookupGetter__()</code></a></li>
<li><a href="Reference/Functions/set" title="当尝试设置属性时set语法将对象属性绑定到要调用的函数。"><code>set</code></a> operator</li>
<li><a href="Reference/Global_Objects/Object/getOwnPropertyDescriptor" title="Object.getOwnPropertyDescriptor() 方法返回指定对象上一个自有属性对应的属性描述符。(自有属性指的是直接赋予该对象的属性,不需要从原型链上进行查找的属性)"><code>Object.getOwnPropertyDescriptor()</code></a> and <a href="Reference/Global_Objects/Object/getPrototypeOf" title="Object.getPrototypeOf() 方法返回指定对象的原型(内部[[Prototype]]属性的值)。"><code>Object.getPrototypeOf()</code></a></li>
<li><a href="Reference/Global_Objects/Object/__defineGetter__" title="__defineGetter__ 方法可以将一个函数绑定在当前对象的指定属性上,当那个属性的值被读取时,你所绑定的函数就会被调用。"><code>Object.prototype.__defineGetter__()</code></a></li>
<li><a href="Reference/Global_Objects/Object/__defineSetter__" title="__defineSetter__ 方法可以将一个函数绑定在当前对象的指定属性上,当那个属性被赋值时,你所绑定的函数就会被调用。"><code>Object.prototype.__defineSetter__()</code></a></li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters">JS Guide: Defining Getters and Setters</a></li>
</ul>
</article>