1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
---
title: Object.prototype.__noSuchMethod__
slug: Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod
translation_of: Archive/Web/JavaScript/Object.noSuchMethod
---
<div>{{JSRef}} {{obsolete_header}}</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) 规范转而采用 {{jsxref("Proxy")}} 对象, 可以实现下面的效果(以及更多)。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><code><var>obj</var>.__noSuchMethod__ = <var>fun</var></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>fun</code></dt>
<dd>函数形式如下:</dd>
<dd>
<pre class="brush: js"><code>function (<var>id</var>, <var>args</var>) { . . . }</code></pre>
<dl>
<dt><code>id</code></dt>
<dd>调用的不存在的方法名</dd>
<dt><code>args</code></dt>
<dd>传递给该方法的参数数组</dd>
</dl>
</dd>
</dl>
<h2 id="描述">描述</h2>
<p>默认情况喜爱,试图调用对象上不存在的方法其结果是在{{jsxref("TypeError")}}上抛出异常,这种行为可以在</p>
<p>By default, an attempt to call a method that doesn't exist on an object results in a {{jsxref("TypeError")}} 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 {{jsxref("Array.prototype")}} 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 class="brush: js">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 ()
</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 class="brush: js">// 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);
}
</pre>
<p>An example of using this idea is shown below.</p>
<pre class="brush: js">// 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
</pre>
<h2 id="Specifications">Specifications</h2>
<p>Not part of any specifications. This feature has been removed, see {{bug(683218)}}.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>{{CompatibilityTable}}</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>{{CompatNo}}</td>
<td>{{CompatNo}} [1]</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</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>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}} [1]</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] This feature was implemented until version 43.</p>
|