函数作用域之闭包与this!

函数基础友情链接:http://speakingjs.com/es5/ch01.html#_functions

作用域链图解

 
var x = 1;
function foo(){
    var y = 2;
  
    function bar(){
        var z = 3;
        alert(x+y+z);
    }
    bar();
}
foo();

  

bar函数的scope chain为[0]bar.AO-->[1]foo.AO-->[2]global.VO

foo函数的scope chain为[0]foo.AO-->[1]global.VO

1.JavaScript 中变量作用域是怎样工作的例子

code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//设置一个等于"test"的全局变量 foo
var foo = "test";
//在 if 块中
if ( true ) {
  //设置 foo 为"new test"
  //注意:这仍然是在全局作用域中
  var foo = "new test";
}
//正如我们在此处可见,foo 现在等于"new test"
alert( foo == "new test" );
//创建一个修改变量 foo 的函数
function test() {
  var foo = "old test";
}
//调用时,foo 却驻留在是在函数的作用域里面
test(); //this指向全局,外部变量没权限直接获取内部“”old test"值
//确认一下,foo 的值仍然是"new test"
alert( foo == "new test" );
 

  2.JavaScript的全局变量 与 window 对象的例子

code:

?
1
2
3
4
//全局变量,包含字符串"test"
var test = "test";
//你会发现,我们的全局变量和 window 的 test 属性是相同的
alert( window.test == test );

   

  3.隐式全局变量声明的示例

code:

?
1
2
3
4
5
6
7
8
//一个为变量 foo 赋值的函数
function test() {
  foo = "test";
}
//调用函数为 foo 赋值
test();
//我们发现 foo 现在是全局变量了
alert( window.foo == "test" );
 
 
闭包作用
 

1.读取、引用、调用函数内部的变量,通过return this做API接口;

2.是让这些变量的值始终保持在内存中(延长变量生命周期)。

 

友情链接:http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html

各种上下文中的this

 

2.this是什么?

  this是关键字,语言规范里规定他指向函数执行时的当前对象。它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。

3.this到底指向哪?

 this永远指向所在函数的所有者,或者说引用触发方法的对象,当没有显示的所有者的时候,那么this指向全局对象。

4.各种情况下的this的具体指向?

(1).全局作用域

console.log( this )
(2).函数作为某个对象的成员方法调用   this>>>>>>>>>>>!windown
?
1
2
3
4
5
6
7
8
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
obj.getName(); //chirenmiao2

  (3).函数作为函数直接使用   this>>>>>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
情况一
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
var getName= obj.getName;
getName();   //chirenmiao1
 
 
情况二:
function myFun() {
     console.log( this );
}
myFun();

  (4).函数作为构造函数调用          this>>>>>>>>>>>>!windown

?
1
2
3
4
5
6
7
8
9
var name = 'chirenmiao1' ;
var Obj = function (x, y) {
     this .name = 'chirenmiao2' ;
}
Obj.prototype.getName = function () {
     console.log( this .name);
}
var myObj = new Obj();
myObj.getName(); //chirenmiao2

  (5).setTimeout和setInterval以及匿名函数              this>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
var name = "chirenmiao1" ;
var obj = {
     name: "chirenmiao2" ,
     getName: function () {
         setTimeout( function () {
             console.log( this .name);
         }, 1000);
     },
};
  
obj.getName(); //chirenmiao1

  

?
1
2
3
( function () {
     console.log( this );
})() //window

  

var x = 1;
function foo(){
    var y = 2;
  
    function bar(){
        var z = 3;
        alert(x+y+z);
    }
    bar();
}
foo();

  

bar函数的scope chain为[0]bar.AO-->[1]foo.AO-->[2]global.VO

foo函数的scope chain为[0]foo.AO-->[1]global.VO

1.JavaScript 中变量作用域是怎样工作的例子

code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//设置一个等于"test"的全局变量 foo
var foo = "test";
//在 if 块中
if ( true ) {
  //设置 foo 为"new test"
  //注意:这仍然是在全局作用域中
  var foo = "new test";
}
//正如我们在此处可见,foo 现在等于"new test"
alert( foo == "new test" );
//创建一个修改变量 foo 的函数
function test() {
  var foo = "old test";
}
//调用时,foo 却驻留在是在函数的作用域里面
test(); //this指向全局,外部变量没权限直接获取内部“”old test"值
//确认一下,foo 的值仍然是"new test"
alert( foo == "new test" );
 

  2.JavaScript的全局变量 与 window 对象的例子

code:

?
1
2
3
4
//全局变量,包含字符串"test"
var test = "test";
//你会发现,我们的全局变量和 window 的 test 属性是相同的
alert( window.test == test );

   

  3.隐式全局变量声明的示例

code:

?
1
2
3
4
5
6
7
8
//一个为变量 foo 赋值的函数
function test() {
  foo = "test";
}
//调用函数为 foo 赋值
test();
//我们发现 foo 现在是全局变量了
alert( window.foo == "test" );

1.读取、引用、调用函数内部的变量,通过return this做API接口;

2.是让这些变量的值始终保持在内存中(延长变量生命周期)。

 

友情链接:http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html

各种上下文中的this

 

2.this是什么?

  this是关键字,语言规范里规定他指向函数执行时的当前对象。它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。

3.this到底指向哪?

 this永远指向所在函数的所有者,或者说引用触发方法的对象,当没有显示的所有者的时候,那么this指向全局对象。

4.各种情况下的this的具体指向?

(1).全局作用域

console.log( this )
(2).函数作为某个对象的成员方法调用   this>>>>>>>>>>>!windown
?
1
2
3
4
5
6
7
8
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
obj.getName(); //chirenmiao2

  (3).函数作为函数直接使用   this>>>>>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
情况一
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
var getName= obj.getName;
getName();   //chirenmiao1
 
 
情况二:
function myFun() {
     console.log( this );
}
myFun();

  (4).函数作为构造函数调用          this>>>>>>>>>>>>!windown

?
1
2
3
4
5
6
7
8
9
var name = 'chirenmiao1' ;
var Obj = function (x, y) {
     this .name = 'chirenmiao2' ;
}
Obj.prototype.getName = function () {
     console.log( this .name);
}
var myObj = new Obj();
myObj.getName(); //chirenmiao2

  (5).setTimeout和setInterval以及匿名函数              this>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
var name = "chirenmiao1" ;
var obj = {
     name: "chirenmiao2" ,
     getName: function () {
         setTimeout( function () {
             console.log( this .name);
         }, 1000);
     },
};
  
obj.getName(); //chirenmiao1

  

?
1
2
3
( function () {
     console.log( this );
})() //window

  

2.this是什么?

  this是关键字,语言规范里规定他指向函数执行时的当前对象。它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。

3.this到底指向哪?

 this永远指向所在函数的所有者,或者说引用触发方法的对象,当没有显示的所有者的时候,那么this指向全局对象。

4.各种情况下的this的具体指向?

(1).全局作用域

console.log( this )
(2).函数作为某个对象的成员方法调用   this>>>>>>>>>>>!windown
?
1
2
3
4
5
6
7
8
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
obj.getName(); //chirenmiao2

  (3).函数作为函数直接使用   this>>>>>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
情况一
var name = "chirenmiao1" ;
var obj= {
     name: "chirenmiao2" ,
     getName: function () {
         console.log( this .name);
     }
}
var getName= obj.getName;
getName();   //chirenmiao1
 
 
情况二:
function myFun() {
     console.log( this );
}
myFun();

  (4).函数作为构造函数调用          this>>>>>>>>>>>>!windown

?
1
2
3
4
5
6
7
8
9
var name = 'chirenmiao1' ;
var Obj = function (x, y) {
     this .name = 'chirenmiao2' ;
}
Obj.prototype.getName = function () {
     console.log( this .name);
}
var myObj = new Obj();
myObj.getName(); //chirenmiao2

  (5).setTimeout和setInterval以及匿名函数              this>>>>>>>>>>>windown

?
1
2
3
4
5
6
7
8
9
10
11
var name = "chirenmiao1" ;
var obj = {
     name: "chirenmiao2" ,
     getName: function () {
         setTimeout( function () {
             console.log( this .name);
         }, 1000);
     },
};
  
obj.getName(); //chirenmiao1

  

?
1
2
3
( function () {
     console.log( this );
})() //window

  

猜你喜欢

转载自www.cnblogs.com/Longhua-0/p/9282875.html