- var a=1;
- var b={
- a:0,
- c:(function(){
- this.a=2;
- a=7;
- console.log("this.a"+this.a);
- console.log("第一层"+a);
- return function(){
- console.log("第二层"+a);
- return this.a=4;
- }
- })()
- };
- console.log("全局"+a);
- console.log("返回值"+b.c());
直接复制了控制台输出
- [Web浏览器] "this.a2" /HelloHBuilder/index.html (22)
- [Web浏览器] "第一层7" /HelloHBuilder/index.html (23)
- [Web浏览器] "全局7" /HelloHBuilder/index.html (30)
- [Web浏览器] "第二层7" /HelloHBuilder/index.html (25)
- [Web浏览器] "返回值4" /HelloHBuilder/index.html (31)
所以this又相当于指向的是当前对象
通过对象来调用函数,this指向了对象,如果把对象的函数变成变量,这个变量属于全局,this指向了window如
- function b(){
- return this;
- }
- var a=b();
- console.log(a)//[object Window]