mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-10 07:54:06 +08:00
119 lines
6.4 KiB
HTML
119 lines
6.4 KiB
HTML
<article id="wikiArticle">
|
||
<div>
|
||
<div class="blockIndicator nonStandard nonStandardHeader">
|
||
<p><strong><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> 非标准</strong><br/>
|
||
该特性是非标准的,请尽量不要在生产环境中使用它!</p>
|
||
</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>
|
||
<h2 id="Summary" name="Summary">概述</h2>
|
||
<p><code><strong>__defineGetter__</strong></code> 方法可以将一个函数绑定在当前对象的指定属性上,当那个属性的值被读取时,你所绑定的函数就会被调用。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><code><var>obj</var>.__defineGetter__(<var>prop</var>, <var>func</var>)</code></code></pre>
|
||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||
<dl>
|
||
<dt>
|
||
<code>prop</code></dt>
|
||
<dd>
|
||
一个字符串,表示指定的属性名。</dd>
|
||
<dt>
|
||
<code>func</code></dt>
|
||
<dd>
|
||
一个函数,当 <code>prop</code> 属性的值被读取时自动被调用。</dd>
|
||
</dl>
|
||
<h2 id="Description" name="Description">描述</h2>
|
||
<p><code>__defineGetter__ 方法可以为一个已经存在的对象设置(新建或修改)访问器属性,</code>而 <a class="new" href="Reference/Operators/get" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!">对象字面量中的 get 语法</a> 只能在新建一个对象时使用。</p>
|
||
<h2 id="Examples" name="Examples"><span class="def"><span>示例</span></span></h2>
|
||
<pre><code class="language-javascript">// 请注意,该方法是非标准的:
|
||
|
||
var o = {};
|
||
o.__defineGetter__('gimmeFive', function() { return 5; });
|
||
console.log(o.gimmeFive); // 5
|
||
|
||
|
||
// 请尽可能使用下面的两种推荐方式来代替:
|
||
|
||
// 1. 在对象字面量中使用 get 语法
|
||
var o = { get gimmeFive() { return 5; } };
|
||
console.log(o.gimmeFive); // 5
|
||
|
||
// 2. 使用 Object.defineProperty 方法
|
||
var o = {};
|
||
Object.defineProperty(o, 'gimmeFive', {
|
||
get: function() {
|
||
return 5;
|
||
}
|
||
});
|
||
console.log(o.gimmeFive); // 5
|
||
</code></pre>
|
||
<h2 id="Specifications" name="Specifications">规范</h2>
|
||
<p>不属于任何规范。</p>
|
||
<h2 id="Browser_compatibility" name="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: #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: #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>
|
||
<h2 id="See_also" name="See_also">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Object/__defineSetter__" title="__defineSetter__ 方法可以将一个函数绑定在当前对象的指定属性上,当那个属性被赋值时,你所绑定的函数就会被调用。"><code>Object.prototype.__defineSetter__()</code></a></li>
|
||
<li><a class="new" href="Reference/Operators/get" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>get</code></a> operator</li>
|
||
<li><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Object/__lookupGetter__" title="__lookupGetter__ 方法会返回当前对象上指定属性的属性读取访问器函数(getter)。"><code>Object.prototype.__lookupGetter__()</code></a></li>
|
||
<li><a href="Guide/Working_with_Objects#Defining_getters_and_setters">JS 指南: 定义 Getter 和 Setter</a></li>
|
||
<li><a class="external" href="http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/" rel="noopener">[个人博文] defineGetter__ 和 __defineSetter__已被废弃</a></li>
|
||
</ul>
|
||
</article> |