fork download
  1. function hoge (str) {
  2. var map = {a:'hoge', b:'foo', c:'piyo'};
  3.  
  4. result = str.replace(/[a-c]/g, function (token) { // クロージャは 変数str, map, result への参照を保持する (window の unload まで保持するのか、関数実行完了まで保持するのか…?)
  5. return map[token];
  6. });
  7.  
  8. str = map = null; // 今まではメモリを占有し続けるのが勿体ないので null を代入していたけど、ひょっとして必要ない…?
  9.  
  10. return result;
  11. }
  12.  
  13. print(hoge('a b c'));
Success #stdin #stdout 0.25s 214016KB
stdin
Standard input is empty
stdout
hoge foo piyo