首先,我的html内容如下:<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>MyXhtml.xhtml</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="example.js">
</script>
</head>
<body>
<h1>SnapShots</h1>
<ul>
<li>
<a href = "images/1.jpg" title = "1" onclick = "showPic(this); return false;">1</a>
</li>
<li>
<a href = "images/2.jpg" title = "2" onclick = "showPic(this); return false;">2</a>
</li>
<li>
<a href = "images/3.jpg" title = "3" onclick = "showPic(this); return false;">3</a>
</li>
</ul>
<p id = "description">choose an image</p>
<img id="placeholder" src = "images/3.jpg" alt="my image gallery"/>

</body>
</html>我的example.js内容如下:for(var propName in window) {
document.write(proName);
document.write("<br/>");
}function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
var text = whichpic.getAttribute("title");
var description = document.getElementById("description");
description.lastChild.nodeValue = text;
}function countBodyChildren() {
var body_element = document.getElementsByTagName("body")[0];
alert(body_element.childNodes.nodeType);
alert(body_element.nodeType);
}window.onload = countBodyChildren;function sum(num1, num2) {
return num1 + num2;
}
alert(sum(10, 10));var anothersum = sum;
alert(anothersum(10,10));sum = null;
alert(anothersum(10, 10));
alert(sum(10, 10));为什么js中的function sum及下面的不能执行,把function sum及以下的写到js文件的最上面就能执行?哪位大侠帮忙解答一下,谢谢!

解决方案 »

  1.   

    还有,就是js中倒数第三行,sum = null,已经让sum和函数断绝关系了,为什么还是可以调用该函数,结果为20?
      

  2.   

    sum是函数,赋值null,这个方法仍在!
      

  3.   

    你说的执行顺序,每个方法加个alert()看下打印顺序不就知道了
      

  4.   

    哦,但是书上写着函数名实际上是一个指向函数对象的指针,函数是对象,那么设置sum = null,这个sum指针就与函数脱离关系了啊?还有前面那个执行顺序的问题是怎么回事呢?
      

  5.   

    我的意思是就是js中只执行到window.onload = countBodyChildren;后面的js都没有执行,为什么呢?
      

  6.   

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>MyXhtml.xhtml</title>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        </head>
        <body>
               .........
        </body>
        <script type="text/javascript" src="example.js"></script>
    </html>
    哥们,这样写吧,不然像你那样写的,body没有加载完就开始执行JS了,找不到DOM,就找错,不好
      

  7.   

    呵呵,谢谢楼上兄弟的提醒,不过这样还是没能解决以上问题,再总结一下:
    1。书上写着函数名实际上是一个指向函数对象的指针,函数是对象,那么设置sum = null,这个sum指针就与函数脱离关系了啊?还有前面那个执行顺序的问题是怎么回事呢?
    2.js中只执行到window.onload = countBodyChildren;后面的js都没有执行,为什么呢?
    请大虾们帮忙解答一下,谢谢!
      

  8.   

    function A(){alert('In a')}
    A=null;
    A();
    测试下就知道了。报错!之所以能看到对话框是anothersum报出的。
      

  9.   

    错误出现在这里    for(var propName in window) {
        document.write(proName);
        document.write("<br/>");
    }
    propName   proName