9

次のような機能がある場合:

function catchUndefinedFunctionCall( name, arguments )
{
    alert( name + ' is not defined' );
}

そして私は愚かなことをします

foo( 'bar' );

fooが定義されていない場合、名前を「foo」、引数を「bar」を含む配列で、catch関数を呼び出す方法はありますか?

4

3 に答える 3

10

とにかく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をご覧ください。

于 2010-03-27T00:25:33.653 に答える
6
try {
 foo();
}
catch(e) {
   callUndefinedFunctionCatcher(e.arguments);
}

更新しました

関数に渡すe.argumentsと、最初に渡そうとしたものが得られます。

于 2010-03-26T23:48:41.013 に答える
0
someFunctionThatMayBeUndefinedIAmNotSure ? someFunctionThatMayBeUndefinedIAmNotSure() : throw new Error("Undefined function call");
于 2010-03-26T23:46:56.840 に答える