易截截图软件、单文件、免安装、纯绿色、仅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!


相关文档:

一家之言:说说 JavaScript 的类成员的访问控制

看书的时候遇到这样一个问题,程序代码如下
var ob = function(){
var obj = this;
function fn1(){
alert( obj === window );//false
alert( this === window );//ture
}
this.fn2 = function() {
fn1();
}

当时很不明白fn1里面第二个alert的结果,为 ......

[翻译]High Performance JavaScript(002)

Grouping Scripts 成组脚本
    Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time ......

[翻译]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(005)

第二章  Data Access  数据访问
    One of the classic computer science problems is determining where data should be stored for optimal reading and writing. Where data is stored is related to how quickly it can be retrieved during code execution. This problem in JavaScri ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号