作者dreamerslab (dreamerslab)
看板Ajax
標題Re: [問題] 在Function中取得Function Name
時間Thu Feb 10 13:46:10 2011
※ 引述《IwillSay (我將會)》之銘言:
: 請問假如我有一個object長這樣
: var fns = {
: MethodA : function() {
: //alert(???); 在這邊取得MethodA的字串
: }
: };
: 請問有辦法在MethodA裡面取得他對應的Property Name嗎(MethodA)
除非你給這個function一個名字
就可以像下面抓出來
var fns = {
MethodA : function MethodA(){
var tmp = /\W*function\s+([\w\$]+)\(/.exec(arguments.callee.toString()),
functionName = tmp !== null ? tmp[ 1 ] : 'anonymous';
alert(functionName);
}
};
fns.MethodA();
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.192.49.219