Skip to content

Instantly share code, notes, and snippets.

@think49
Created November 5, 2010 16:10
Show Gist options
  • Save think49/664373 to your computer and use it in GitHub Desktop.
Save think49/664373 to your computer and use it in GitHub Desktop.
getFunctionName.js : Function#name を取得する
// getFunctionName.js
function getFunctionName (func) {
var functionBody;
functionBody = /^function\s+([^\s(]*)(?=\()/.exec(func.toString());
return functionBody ? functionBody[1] : null; // Function#name || null
}
@think49
Copy link
Author

think49 commented Nov 5, 2010

本来は 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment