Javascriptの小さなデバッガー

トリムパスプロジェクトの研究で最近遭遇しました。 個人的には、ブレークポイントを設定する興味深い方法を除いて、自分には何も面白いものは見つかりませんでした(オリジナルから少し変更しました-そうしないと、オペラとIAで長い行が誤って表示されました)



breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  1. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  2. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  3. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  4. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  5. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  6. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  7. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  8. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  9. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  10. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  11. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  12. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  13. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  14. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  15. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  16. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  17. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  18. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  19. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  20. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  21. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  22. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  23. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  24. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  25. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  26. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  27. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  28. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  29. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  30. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  31. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  32. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  33. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



  34. breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .



breakpoint = function (_name, _context) { if (!breakpoint.allow) { return ; } var expr, result, output; while ( true ) { expr = prompt( 'Debug into breakpoint "' + _name + '"' , '' ); if (!expr) { break ; } else { output = true ; try { result = result = _context(expr); } catch (e) { alert( '[!] caught exception with message "' + e.message + '"' ); output = false ; } if (output) { alert(result); } } } } breakpoint.allow = true ; * This source code was highlighted with Source Code Highlighter .







ここで、ブレークカレントを設定するには、次のように記述する必要があります。







  1. 関数 f(_a、_b、_c)
  2. {
  3. var a = 10;
  4. var b = 20;
  5. var c = 30;
  6. ブレークポイント( "breakpoint1"関数 (_ $){ return eval(_ $);});
  7. }
  8. f(40、50、60);
*このソースコードは、 ソースコードハイライターで強調表示されました。




関数function(_ $){return eval(_ $);に注意してください。 }-これは、呼び出し元関数のコンテキストがブレークポイントに渡される方法です。 名前_ $はまれな識別子として使用されます。



このメカニズムは、たとえばIAにfirebugやその他の通常のデバッガーがない場合に役立ちます。 シンプルで上品。 :^)



だまし書きを書いた場合は謝罪します-私にとってこれは新しいアプローチでした。



UPD#1 12/08/09



このようにブレークポイントコードを変更した場合:







  1. debug = true ;
  2. ブレークポイント= '(新しいブレークポイント(\'匿名\ '))。exec(関数(_ $){return eval(_ $);});' ;
  3. ブレークポイント= 関数 (_name)
  4. {
  5. この .name = _name? '匿名' :_name;
  6. this .allow = true ;
  7. }
  8. Breakpoint.prototype.exec = function (_context)
  9. {
  10. if (!(デバッグ&& this .allow))
  11. {
  12. 帰る
  13. }
  14. ...
  15. }
*このソースコードは、 ソースコードハイライターで強調表示されました。




次のように書くことができます:







  1. eval(ブレークポイント);
*このソースコードは、 ソースコードハイライターで強調表示されました。




アイデアをありがとうasmolianinov (コメントを参照)。



All Articles