次のような機能がある場合:
function catchUndefinedFunctionCall( name, arguments )
{
alert( name + ' is not defined' );
}
そして私は愚かなことをします
foo( 'bar' );
fooが定義されていない場合、名前を「foo」、引数を「bar」を含む配列で、catch関数を呼び出す方法はありますか?
次のような機能がある場合:
function catchUndefinedFunctionCall( name, arguments )
{
alert( name + ' is not defined' );
}
そして私は愚かなことをします
foo( 'bar' );
fooが定義されていない場合、名前を「foo」、引数を「bar」を含む配列で、catch関数を呼び出す方法はありますか?
とにかくMozillaJavascript1.5にあります(これは非標準です)。
これをチェックしてください:
var myObj = {
foo: function () {
alert('foo!');
}
, __noSuchMethod__: function (id, args) {
alert('Oh no! '+id+' is not here to take care of your parameter/s ('+args+')');
}
}
myObj.foo();
myObj.bar('baz', 'bork'); // => Oh no! bar is not here to take care of your parameter/s (baz,bork)
かなりクール。詳細については、https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/NoSuchMethodをご覧ください。
try {
foo();
}
catch(e) {
callUndefinedFunctionCatcher(e.arguments);
}
更新しました
関数に渡すe.arguments
と、最初に渡そうとしたものが得られます。
someFunctionThatMayBeUndefinedIAmNotSure ? someFunctionThatMayBeUndefinedIAmNotSure() : throw new Error("Undefined function call");