js中不带var的变量申明

js的一些变量命名规则:

(1)首字符不能是数字、可以是美元符号 $ 以及下划线 _ 
(2)由字母、数字、下划线、美元符号组成 
(3)区分大小写 
注意:在函数中定义变量时,没写  var  则是全局变量。

 

HTML代码:

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head>
 3     <title></title>
 4     <script type="text/javascript">
 5 
 6         function fn1() {
 7             _test = "Hello world";
 8         }
 9         function fn2() {
10             fn1();
11             alert(_test);
12         }   
13     </script>
14 </head>
15 <body>
16 <input type="button"  onclick="fn2()"  value="ciclk me"/>
17 </body>
18 </html>

先调用fn1,给变量_test赋值,然后直接在fn2里面显示,能成功的显示,可以证明此时的_test是全局变量。

显示结果:

 

posted @ 2012-08-11 15:53  st_gloden  阅读(875)  评论(0编辑  收藏  举报