js执行的顺序问题

hero_winner 2010-08-17 02:38:47
首先,我的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文件的最上面就能执行?哪位大侠帮忙解答一下,谢谢!
...全文
126 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
laborc1987 2010-08-20
  • 打赏
  • 举报
回复

正解
[Quote=引用 9 楼 kc8886 的回复:]
错误出现在这里 for(var propName in window) {
document.write(proName);
document.write("<br/>");
}
propName proName
[/Quote]
kc8886 2010-08-18
  • 打赏
  • 举报
回复
错误出现在这里 for(var propName in window) {
document.write(proName);
document.write("<br/>");
}
propName proName
etherdream 2010-08-17
  • 打赏
  • 举报
回复
function A(){alert('In a')}
A=null;
A();
测试下就知道了。报错!之所以能看到对话框是anothersum报出的。
hero_winner 2010-08-17
  • 打赏
  • 举报
回复
呵呵,谢谢楼上兄弟的提醒,不过这样还是没能解决以上问题,再总结一下:
1。书上写着函数名实际上是一个指向函数对象的指针,函数是对象,那么设置sum = null,这个sum指针就与函数脱离关系了啊?还有前面那个执行顺序的问题是怎么回事呢?
2.js中只执行到window.onload = countBodyChildren;后面的js都没有执行,为什么呢?
请大虾们帮忙解答一下,谢谢!
cloudapex 2010-08-17
  • 打赏
  • 举报
回复
<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,就找错,不好
hero_winner 2010-08-17
  • 打赏
  • 举报
回复
我的意思是就是js中只执行到window.onload = countBodyChildren;后面的js都没有执行,为什么呢?
hero_winner 2010-08-17
  • 打赏
  • 举报
回复
哦,但是书上写着函数名实际上是一个指向函数对象的指针,函数是对象,那么设置sum = null,这个sum指针就与函数脱离关系了啊?还有前面那个执行顺序的问题是怎么回事呢?
jinjin198722 2010-08-17
  • 打赏
  • 举报
回复
你说的执行顺序,每个方法加个alert()看下打印顺序不就知道了
jinjin198722 2010-08-17
  • 打赏
  • 举报
回复
sum是函数,赋值null,这个方法仍在!
hero_winner 2010-08-17
  • 打赏
  • 举报
回复
还有,就是js中倒数第三行,sum = null,已经让sum和函数断绝关系了,为什么还是可以调用该函数,结果为20?

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧