再帰を使うほうが難しいのでは?
- 教えてgooの答えを見てから
http://okwave.jp/qa/q5862843.html
<p id="Target"><span><span><span><span><span><span>Hello, World!</span></span></span></span></span></span></p> <script type="text/javascript"> (function(){ function Hello(HTMLElement){ var firstChild = HTMLElement.firstChild; var result; switch(firstChild.nodeType){ case 1: // Element Node result = Hello(firstChild); // 再帰呼び出し break; case 3: // Text Node result = firstChild.nodeValue; break; } return result; } var text = Hello(document.getElementById('Target')); alert(text); // Hello, World! })(); </script>
のHelloを、
function Hello( HTMLElement ) { var e = HTMLElement, b; while( e = e.firstChild ) b = e; return 3 == b.nodeType ? b.nodeValue: null; }
にしてはどうだろう?
お題が「自身の関数オブジェクトを参照するには?」なので、ずれているので、ためらいました。
例題のタグには、隙間がないのでよいのかもしれないけど・・・あれば微妙。