全局变量遇到的坑

这种情况结果一样
var aname="The Window";   

console.log(aname);//The Window
console.log(window.aname)//The Window


但是写在$(function(){........})中结果不等
$(function(){
var aname="The Window";   

console.log(aname);//The Window
console.log(window.aname)//underfind


});
案例二

var aname="The Window";   
var obj = {   
    aname:"My Object",   
    getNameFunc : function(){  
        return function(){   
            alert(this.aname);   
        };   
    }   
};
obj.getNameFunc()();//The Window
obj.getNameFunc()();//The Window

和

$(function(){

var aname="The Window";   
var obj = {   
    aname:"My Object",   
    getNameFunc : function(){  
        return function(){   
            alert(this.aname);   
        };   
    }   
};
obj.getNameFunc()//这里面的this位window
obj.getNameFunc()();//underfind  })

结果是因为作用于所引起的

详见:http://q.cnblogs.com/q/82633/

posted @ 2016-05-24 12:06  还能再菜点吗?  阅读(191)  评论(0编辑  收藏  举报