mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
189 lines
8.3 KiB
HTML
189 lines
8.3 KiB
HTML
<article id="wikiArticle">
|
||
<div> <div class="blockIndicator obsolete obsoleteHeader"><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/>This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.</p></div></div>
|
||
<p><strong><code>__noSuchMethod__</code></strong> 属性曾经是指当调用某个对象里不存在的方法时即将被执行的函数,但是现在这个函数已经不可用。</p>
|
||
<p><code><font face="Open Sans, Arial, sans-serif">在</font><strong>__noSuchMethod__</strong></code> 属性被移除之后,ECMAScript 2015 (ES6) 规范转而采用 <a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。"><code>Proxy</code></a> 对象, 可以实现下面的效果(以及更多)。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript"><code><var>obj</var>.__noSuchMethod__ = <var>fun</var></code></code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt><code>fun</code></dt>
|
||
<dd>函数形式如下:</dd>
|
||
<dd>
|
||
<pre><code class="language-javascript"><code>function (<var>id</var>, <var>args</var>) { . . . }</code></code></pre>
|
||
<dl>
|
||
<dt><code>id</code></dt>
|
||
<dd>调用的不存在的方法名</dd>
|
||
<dt><code>args</code></dt>
|
||
<dd>传递给该方法的参数数组</dd>
|
||
</dl>
|
||
</dd>
|
||
</dl>
|
||
<h2 id="描述">描述</h2>
|
||
<p>默认情况喜爱,试图调用对象上不存在的方法其结果是在<a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>上抛出异常,这种行为可以在</p>
|
||
<p>By default, an attempt to call a method that doesn't exist on an object results in a <a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> being thrown. This behavior can be circumvented by defining a function at that object's <code>__noSuchMethod__</code> member. The function takes two arguments, the first is the name of the method attempted and the second is an array of the arguments that were passed in the method call. The second argument is an actual array (that is, it inherits through the <a href="Reference/Global_Objects/Array/prototype" title="Array.prototype 属性表示 Array 构造函数的原型,并允许您向所有Array对象添加新的属性和方法。"><code>Array.prototype</code></a> chain) and not the array-like <a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments" title="JavaScript/Reference/Functions/arguments">arguments object</a>.</p>
|
||
<p>If this method cannot be called, either as if <code>undefined</code> by default, if deleted, or if manually set to a non-function, the JavaScript engine will revert to throwing <code>TypeError</code>s.</p>
|
||
<h2 id="Examples">Examples</h2>
|
||
<h3 id="Simple_test_of___noSuchMethod__">Simple test of <code>__noSuchMethod__</code></h3>
|
||
<pre><code class="language-javascript">var o = {
|
||
__noSuchMethod__: function(id, args) {
|
||
console.log(id, '(' + args.join(', ') + ')');
|
||
}
|
||
};
|
||
|
||
o.foo(1, 2, 3);
|
||
o.bar(4, 5);
|
||
o.baz();
|
||
|
||
// Output
|
||
// foo (1, 2, 3)
|
||
// bar (4, 5)
|
||
// baz ()
|
||
</code></pre>
|
||
<h3 id="Using___noSuchMethod___to_simulate_multiple_inheritance">Using <code>__noSuchMethod__</code> to simulate multiple inheritance</h3>
|
||
<p>An example of code that implements a primitive form of multiple inheritance is shown below.</p>
|
||
<pre><code class="language-javascript">// Doesn't work with multiple inheritance objects as parents
|
||
function noMethod(name, args) {
|
||
var parents = this.__parents_;
|
||
|
||
// Go through all parents
|
||
for (var i = 0; i < parents.length; i++) {
|
||
// If we find a function on the parent, we call it
|
||
if (typeof parents[i][name] == "function") {
|
||
return parents[i][name].apply(this, args);
|
||
}
|
||
}
|
||
|
||
// If we get here, the method hasn't been found
|
||
throw new TypeError;
|
||
}
|
||
|
||
// Used to add a parent for multiple inheritance
|
||
function addParent(obj, parent) {
|
||
// If the object isn't initialized, initialize it
|
||
if (!obj.__parents_) {
|
||
obj.__parents_ = [];
|
||
obj.__noSuchMethod__ = noMethod;
|
||
}
|
||
|
||
// Add the parent
|
||
obj.__parents_.push(parent);
|
||
}
|
||
</code></pre>
|
||
<p>An example of using this idea is shown below.</p>
|
||
<pre><code class="language-javascript">// Example base class 1
|
||
function NamedThing(name){
|
||
this.name=name;
|
||
}
|
||
|
||
NamedThing.prototype = {
|
||
getName: function() { return this.name; },
|
||
setName: function(newName) { this.name = newName; }
|
||
}
|
||
|
||
// Example base class 2
|
||
function AgedThing(age) {
|
||
this.age = age;
|
||
}
|
||
|
||
AgedThing.prototype = {
|
||
getAge: function() { return this.age; },
|
||
setAge: function(age) { this.age = age; }
|
||
}
|
||
|
||
// Child class. inherits from NamedThing and AgedThing
|
||
// as well as defining address
|
||
function Person(name, age, address){
|
||
addParent(this, NamedThing.prototype);
|
||
NamedThing.call(this, name);
|
||
addParent(this, AgedThing.prototype);
|
||
AgedThing.call(this, age);
|
||
this.address = address;
|
||
}
|
||
|
||
Person.prototype = {
|
||
getAddr: function() { return this.address; },
|
||
setAddr: function(addr) { this.address = addr; }
|
||
}
|
||
|
||
var bob = new Person("bob", 25, "New York");
|
||
|
||
console.log("getAge is " + (("getAge" in bob) ? "in" : "not in") + " bob");
|
||
// getAge is not in bob
|
||
|
||
console.log("bob's age is: " + bob.getAge());
|
||
// bob's age is: 25
|
||
|
||
console.log("getName is " + (("getName" in bob) ? "in" : "not in") + " bob");
|
||
// getName is not in bob
|
||
|
||
console.log("bob's name is: " + bob.getName());
|
||
// bob's name is: bob
|
||
|
||
console.log("getAddr is " + (("getAddr" in bob) ? "in" : "not in") + " bob");
|
||
// getAddr is in bob
|
||
|
||
console.log("bob's address is: " + bob.getAddr());
|
||
// bob's address is: New York
|
||
</code></pre>
|
||
<h2 id="Specifications">Specifications</h2>
|
||
<p>Not part of any specifications. This feature has been removed, see <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=683218" rel="noopener" title="FIXED: can we remove __noSuchMethod__, use proxies instead?">bug 683218</a>.</p>
|
||
<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: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span> [1]</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</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: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span> [1]</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>[1] This feature was implemented until version 43.</p>
|
||
</article> |