0

try / catch を使用して一般的なデバッグ ルーチンを作成したいのですが、catch セグメント内に、関数名と渡されたすべてのパラメーター (名前 => 値) をログに記録するコードが必要です。

これは可能ですか?

try{
    // code
} catch(e) {
    var function_name = ''; // how can I get this?
    var function_params = ''; // how can I get this?
    var errorText = 'UNEXPECTED ERROR: \n\n Error Details: '+e.toString();
    errorText = errorText+' Called:'+function_name+'('+function_params+')';
}
4

3 に答える 3

0

arguments.callee関数名に使用してみてください

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee

于 2012-11-06T16:40:12.380 に答える
0

Chrome と Firefox では使用できますe.stackが、Internet Explorer ではそのような運はありません。

プロパティは、stackループできる配列です。ブラウザーによって多少異なる場合がありますが、読み取り可能なスタックトレースを作成するのはそれほど難しくありません。

現在、すべてのブラウザーで引数をキャッチすることはできません。

Chrome では、以下を使用する必要がある場合があります。

Error.prepareStackTrace = function (error, stack) {
    return stack;
};  // augments Chrome's Error.stack property with context data
于 2012-11-06T16:35:46.543 に答える
0

関数に名前を付けていないため、これは正確な答えではありませんが、これは関数本体全体を返します...

作業サンプル: http://jsfiddle.net/sS6sY/

Object.prototype.getArgs = function(){
//returns the arguments passed to a function.        
var els = [];
        for(i=0; i< this.arguments.length; i++){
            els.push(this.arguments[i]);
        }

        return els.join(',')
}

Object.prototype.getMethod = function(){
    //returns the function as a string.
    return this.constructor;

}

var Foo = function(){
   this.arguments = arguments;
   try{
        throw {
            name: 'Oops',
            message: 'Didn\'t mean to make a Foo!'
        }
    }
    catch(e){
        console.log(this.getArgs());
        console.log(this.getMethod());
    }
   }
于 2012-11-06T17:50:26.727 に答える