以ä¸ã«ããã®ã§ãé©å½ã«ä½¿ã£ã¦ãã ããï¼
ååå ¬éããJSãä¸è¨ã«ç½®ãã¾ããã
- ãã¢ãã¼ã¸ï¼http://sugamasao.dip.jp/lang/js/debug/debug.html
- JSã®ç½®ãå ´ï¼http://sugamasao.dip.jp/lang/js/debug/debug.js
ååå ¬éããã¹ã¯ãªããããã®å¤æ´ç¹
- function ã ã£ãå ´åã¯ä¸èº«ã表示ããªãï¼åºã¦ã大ä½ããããã ããªã®ã§ã»ã»ã»ï¼
- ãã¾ãã«æ·±ãé層ã«ãªã£ãå ´åã¯èªéããï¼ã¨ãããã5é層ã¾ã§è¡ãã¨æ·±è¿½ãããªãããã«ããï¼
æ°ã«é£ããªãé¨åçãããã°ãããªã«ãã¦ãã ããã
ããã使ãã¨
ãããªãã¼ã¿æ§é ã®ãªãã¸ã§ã¯ãã
var array = new Array(5, 6); var func = function(){return "test"} var data = { func2:func, // ä¸ã§å®£è¨ãã function ã ã func:func , // åãã Array:array , // ä¸ã§å®£è¨ãã Array ã ã hoge:true, fuga:{test:"test", test2:10, piyo:[10, 20]} }
ãããªé¢¨ã«è¡¨ç¤ºãã¦ããã¾ãã
func2:(function) func:(function) Array { __0:5(number) __1:6(number) } hoge:true(boolean) fuga { __test:test(string) __test2:10(number) __piyo __{ ____0:10(number) ____1:20(number) __} }
ä½¿ãæ¹
ãããªæãã§ãªãã¸ã§ã¯ããçæãã¦ã¡ã½ãããå¼ã¹ã°ãkã
var d = new Debug(); d.writeHTML(hogehoge); // hogehoge ã¨ãã夿°ãã¤ã³ã¹ãã¯ãããï¼HTMLã«åºåï¼ d.write(hogehoge); // åä¸ï¼alert ã§è¡¨ç¤ºããçï¼
ã½ã¼ã¹ã¯ãã¡ã
function Debug() { this.initialize.apply(this, arguments); } Debug.prototype = { initialize: function() { this.SEP = "__"; this.BR = '\n' this.HTML_ID = 'debug_write_html'; }, inspect: function(obj, dep) { var str = ""; if (typeof(dep) == 'undefined') { dep = 0; } else if (dep > 5) { return str; } for (var i in obj) { var seps = ""; for(var j = 0; j < dep; j++) { seps += this.SEP; } if (typeof(obj[i]) == 'function') { str += seps + i + ":(" + typeof(obj[i]) + ")" + this.BR } else if (typeof(obj[i]) == 'object') { str += seps + i + this.BR str += seps + "{" + this.BR + this.inspect(obj[i], dep+1); str += seps + "}" + this.BR; } else { str += seps + i + ":" + obj[i] + "("+ typeof(obj[i]) + ")" + this.BR; } } return str; }, writeHTML: function(data) { var s = this.inspect(data); var t = document.getElementById(this.HTML_ID); if (!t) { var b = document.getElementsByTagName("body")[0]; t = this.createHTML(); b.appendChild(t); } if(t.innerHTML != ""){ s = t.innerHTML + "<hr>" + s; } t.innerHTML = s.replace(new RegExp(this.BR, 'g'), "<br>"); }, write: function(data) { alert(this.inspect(data)); }, createHTML: function() { var t = document.createElement("div"); t.setAttribute('id', this.HTML_ID); t.style.backgroundColor = "#ffffaa"; t.style.borderStyle = 'solid'; t.style.borderColor = '#000000'; t.style.borderWidth = '1px'; t.style.margin = '0.5em'; t.style.padding = '0.5em'; t.style.position = 'fixed'; t.style.right = '0px'; t.style.top = '0px'; t.style.overflow = 'auto'; t.style.height = "90%"; t.style.width = "20%"; t.style.zIndex = "99"; return t; } };