Created
November 5, 2010 16:10
-
-
Save think49/664373 to your computer and use it in GitHub Desktop.
getFunctionName.js : Function#name を取得する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// getFunctionName.js | |
function getFunctionName (func) { | |
var functionBody; | |
functionBody = /^function\s+([^\s(]*)(?=\()/.exec(func.toString()); | |
return functionBody ? functionBody[1] : null; // Function#name || null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
本来は IdentifierName生成規則に従った正規表現パターンを書くべきですが、解読に時間がかかりそうだったので省略しました。
ES3 準拠に従う UA なら FunctionDeclaration 生成規則に従うはずなので現在のパターンでも問題はないと思います。
13 関数定義 (Function Definition) - ES3(ja)
http://www2u.biglobe.ne.jp/~oz-07ams/prog/ecma262r3/13_Function_Definition.html
Identifier - ES3(ja)
http://www2u.biglobe.ne.jp/~oz-07ams/prog/ecma262r3/7_Lexical_Conventions.html#Identifier
下記コードを参考にしました。
JavaScript の質問用スレッド vol.80
http://hibari.2ch.net/test/read.cgi/hp/1284091954/947n-
Ideone.com | Cxjqs
http://ideone.com/Cxjqs