1.注册可以直接调用f()中的b(),c(),d() .原因?自己想。

<!DOCTYPE html>
<html>
<head>
<meta charset=gb2312 />
<title>js</title>
<script>
function f(){
var a = 1;
b = function(){
alert(a);
}
c = function(){
a++;
}
d = function(x){
a = 100;
}
}
</script>
</head> <body>
<input type="button" value="f" onclick="f()"/> //需要先点击这个,才能点击其他,否则出错
<input type="button" value="alert a" onclick="b()"/> <input type="button" value="a++" onclick="c()"/>
<input type="button" value="a=100" onclick="d()"/> </body>
</html>

2.f()改为匿名函数----载入自执行

 <!DOCTYPE html>
<html>
<head>
<meta charset=gb2312 />
<title>js</title>
<script>
(function(){
var a = 1;
b = function(){
alert(a);
}
c = function(){
a++;
}
d = function(x){
a = 100;
}
})();
</script>
</head> <body>
<div>f()改为匿名函数,载入后自执行</div>
<input type="button" value="alert a" onclick="b()"/> <input type="button" value="a++" onclick="c()"/>
<input type="button" value="a=100" onclick="d()"/> </body>
</html>
05-11 16:16