2011-07-05 34 views
16

我有這樣一段代碼當前函數名稱:可以在javascript

function MyFunction() 
{ 
    $.ajax({ 
     type: "POST", 
     url: "ajax.php", 
     dataType: "json", 
     data: "foo=bar", 
     error:function(XMLHttpRequest, textStatus, errorThrown) 
     { 
      alert(arguments.callee); 
     }, 
     success: function(jsonObject) 
     { 
      //do something 
     } 
    }); 
} 

什麼,我想要的是德錯誤scoope內的警報顯示功能名稱,在這種情況下,「MyFunction的」,而是我get是錯誤:函數。

我該如何做到這一點?

+1

我必須失去了一些東西記錄功能。爲什麼不寫'alert('MyFunction');'? –

+0

爲什麼你需要獲取函數的名字?可能你正在接近錯誤的問題。 – Amrit

+2

我想要一些我可以在幾個函數中使用的代碼片段。這樣我可以顯示一個錯誤,並知道錯誤發生在哪個函數中,而不用硬編碼該函數的名稱 –

回答

18

這 -

var my_arguments; 

function MyFunction() { 
    my_arguments = arguments; 

    $.ajax({ 
     type: "POST", 
     url: "http://www.google.com", 
     dataType: "json", 
     data: "foo=bar", 
     error:function(XMLHttpRequest, textStatus, errorThrown) { 
      alert(my_arguments.callee.name); 
     }, 
     success: function(jsonObject) { 
      //do something 
     } 
    }); 
} 

是你需要的。

arguments裏面的error函數指的是這個方法自己的arguments對象。它不涉及MyFunction的對象arguments。這就是爲什麼你得到error:MyFunction。在這種情況下使用全局變量可以爲您解決此問題。

此外,要獲取函數的名稱,您需要使用arguments.callee.namearguments.callee將給你一個調用函數的引用,而不是字符串中的函數名稱。

1

您正在使用arguments範圍內的錯誤功能。你需要這樣的事情,如果你想的MyFunction()的arguments

function MyFunction() 
{ 
    var myfunction_arguments = arguments; 

    $.ajax({ 
     type: "POST", 
     url: "ajax.php", 
     dataType: "json", 
     data: "foo=bar", 
     error:function(XMLHttpRequest, textStatus, errorThrown) 
     { 
      alert(myfunction_arguments.callee); 
     }, 
     success: function(jsonObject) 
     { 
      //do something 
     } 
    }); 
} 

那是什麼意思?


+0

謝謝!你和Sayem Ahmed真的幫助我 –

6

你想要的是arguments.callee.name不過你的情況在上下文中的功能不再MyFunction,但宣佈爲錯誤處理程序的匿名方法...

-2

嘗試

  error:function(xhr){ 
       alert('Error: '+xhr.status+' '+xhr.statusText); 
      } 
-1
alert('MyFunction'); 

你不能這樣做不是更好可靠,因爲該功能幾乎無關,與錯誤處理程序:錯誤處理程序可以很可能已經在完全另一種情況下創建一個封閉MyFunction,或傳遞到它等。

MyFunction不會調用處理程序,所以查詢arguments.*不會幫助。雖然是假設具有相同名稱的任何值都不會發生碰撞(一個合理的假設在許多情況下,而在其他不那麼合理的)

function MyFunction(){ 
    var _injectedFunctionName = arguments.callee.name; 

    ... 
    // inside the error handler 
    alert(_injectedFunctionName); 
    ... 
} 

你可以注入一個值範圍。

事實上,你想要的功能名稱可能是一個很好的跡象表明你錯誤地思考問題。也許如果你解釋你想要做的更廣泛的事情,社區可以提出替代方案。

4

在嚴格模式下,您不能再引用被調用者(請參閱Javascript: is the arguments array deprecated?上的相關討論),因此只有在您沒有使用嚴格模式時,@Sayem的答案纔有效。一般而言,您想調用嚴格模式,因爲它對代碼的可靠性有好處(請參閱@JohnResig article)。

我仍然想有一種方法來引用當前函數的名稱,僅僅是爲了拋出帶有上下文的錯誤消息。例如。

throw myFunctionName()+':invalid number of arguments';

現在我不得不在硬件代碼中的函數名稱。我移動了代碼並忘記更新硬編碼的函數名稱。

我想過一個編碼約定,將THIS_FUNCTION_NAME ='foobar'作爲在函數頭部定義的const,但當然可以移動並且不會更新。

我想只要你有一個堆棧跟蹤可用的投擲點是毫無意義的。

3

時間由新鮮的答案:)

使用console.trace()來刷新這個線程;

Just call console.trace() and Firebug will write a very informative stack trace to the console. Not only will it tell you which functions are on the stack, but it will include the value of each argument that was passed to each function. You can click the functions or objects to inspect them further.

的JavaScript示例:

function fn1(arg1, arg2) { 
    var arg3 = 'myArg3'; 
    fn2(arg1, arg2, arg3); 
} 
function fn2(arg1, arg2, arg3) { 
    var obj = { 
     attr1 : 'val1', 
     attr2 : 'val2' 
    }; 
    console.trace(obj); 
} 
fn1('myArg1', 'myArg2', 'myArg3'); 


火狐控制檯(版本:43.0.1):

Firefox console (version: 43.0.1)


谷歌瀏覽器的控制檯(版本:47.0.2526.106米):

Google Chrome console (version: 47.0.2526.106 m)

你可以找到所有here,你需要

HTH