廃止
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.
概要
__noSuchMethod__ プロパティは、オブジェクトで定義されていない関数を呼び出した時に実行される関数を表しますが、この関数は使用できなくなりました。
__noSuchMethod__ は廃止しましたが、ECMAScript 2015 (ES6) 仕様には Proxy オブジェクトがあり、以下のようなことを (それ以上のことも) 実現できます。
構文
obj.__noSuchMethod__ = fun
引数
fun- 以下の引数をとる関数
function (id, args) { . . . }id- 呼び出された未定義の関数の名前
args- その関数へ渡された引数の配列
説明
オブジェクトで定義されていない関数の呼び出しを試みても、標準の挙動では TypeError 例外が投げられるだけです。オブジェクトのメンバー関数として __noSuchMethod__ を定義しておく事でこの挙動を回避できます。__noSuchMethod__ として定義される関数は 2 つの引数をとります。 1 つ目の引数は呼び出しが試みられた関数の名前を表し、 2 つ目の引数はその関数へ渡された引数の配列を表します。この 2 つ目の引数は実配列です。配列を模したオブジェクトである arguments オブジェクトではありません。(Array.prototypeを継承した Array インスタンスの事を実配列と言います。)
__noSuchMethod__ が undefined であるかデリートされている、あるいは関数以外である場合には、JavaScript エンジンは通常どおり TypeError 例外を投げます。
例
例: __noSuchMethod__ の基本的な使い方
var o = {
__noSuchMethod__: function(id, args) {
console.log(id, '(' + args.join(', ') + ')');
}
};
o.foo(1, 2, 3);
o.bar(4, 5);
o.baz();
// 出力
// foo (1, 2, 3)
// bar (4, 5)
// baz ()
__noSuchMethod__ を使用して多重継承をシミュレートする
多重継承の基本形を実装するコードの例を以下に示します。
// 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);
}
使用例を以下に示します。
// 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
仕様
どの仕様にも含まれていません。この機能は削除されました。バグ 683218 をご覧ください。
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 未サポート | 未サポート [1] | 未サポート | 未サポート | 未サポート |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | 未サポート | 未サポート | 未サポート [1] | 未サポート | 未サポート | 未サポート |
[1] この機能は、バージョン 43 まで実装していました。