易截截图软件、单文件、免安装、纯绿色、仅160KB

Javascript变量作用域

请先看下题:
if(true)
{
     a = 1;
}
alert(a);  //输出啥?
if(true)
{
    var a = 1; 
}
alert(a);  //这又输出什么?
在firefox3.5下,第一个输出1;第二个输出undefined!!!
为啥呢?呵呵,本人在此班门弄斧一下:
一般情况下缺省var声明时,默认该变量为全局变量。了解这句话,所有问题也就迎刃而解了!呵呵!
第一道题:变量a缺省var声明,而直接使用,故而默认为全局变量,输出1
第二道题:变量a,在if块中var声明,属局部变量,输出undefined!


相关文档:

利用http的referer头和Servlet隐藏JavaScript代码

1.       定义一个用于输出JavaScript代码 的Servlet类。
package com.mycompany.response.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServle ......

[翻译]High Performance JavaScript(003)

Dynamic Script Elements  动态脚本元素
    The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrie ......

[翻译]High Performance JavaScript(004)

XMLHttpRequest Script Injection  XHR脚本注入
    Another approach to nonblocking scripts is to retrieve the JavaScript code using an XMLHttpRequest (XHR) object and then inject the script into the page. This technique involves creating an XHR object, downloading the JavaScript f ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号