Ext JS学习第四天 我们所熟悉的javascript(三)

此文用来记录学习笔记;

•javascript之函数
•this关键字的使用
–this关键字总是指向调用者,谁调用函数,this就指向谁
•call、apply的使用
–call和apply主要应用与框架底层,用于绑定函数的执行环境/作用域
•块的概念
–和高级程序语言不同,js里没有块的概念,我们一般用小括号包裹块级作用域
•闭包:掌握闭包必须要深入清楚的概念
–执行环境
–作用域链
–垃圾回收机制
 
附上栗子 代码
 1 // This 关键字 在javascript里的使用
 2 //this关键字总是指向调用者,谁调用函数,this就指向谁
 3 var x = 5; // window.x = 5 ;
 4 function test(){
 5     alert(this.x);    
 6 };
 7 test.x = 10;//给函数加以个静态变量X
 8 alert(test.x);//10
 9 window.test();// 5
10 
11 
12 // call apply 主要作用是绑定作用域
13 var color = 'red';
14 var obj = {color:'yellow'};
15 
16 function showColor(x,y,z){
17     alert(x+y+z);
18     alert(this.color);
19 };
20 //// call、apply 绑定一个函数 ,到你指定的作用域内执行
21 showColor.call(window, 10, 20, 30);//red 60
22 showColor.call(obj, 10, 20, 30);//yellow 60
23 showColor.apply(obj, [2, 3, 4]);//yellow 9
24 
25 //块的概念:
26 function test(){
27     (function(){
28         for(var i = 1 ;i <=5 ; i++){
29             alert(i);
30         }    
31     })();
32     alert(i);//undefined
33 };
34 test();
35 
36 // 闭包 :执行环境、作用域链、js垃圾回收
37 // 函数碰到return直接返回、没有renturn 返回结果undefined
38 // 在javascript语言里: 更不提倡使用全局变量 (1:不安全、2:做变量搜索查找的时候效率比较低)
39 function test(){
40     return function(){
41         alert('我是闭包体代码...');
42     };
43 };
44 var f = test();
45 alert(typeof f);//function
46 f();//我是闭包体代码...
47 
48 function test(){
49     var temp = 10 ;
50     return function(){
51         alert(temp);
52     };
53 };
54 test()();
55 
56 
57 // 避免使用全局变量 点击按钮 统计点击的次数
58 var inp = (function(){
59     var i = 0;
60     return {
61         getCount:function(){
62             alert(++i);
63         }
64     };
65 })();

 推荐给各位推荐个优美文章网www.fishcmonkey.com,学习之余提高文学修养

原文地址:https://www.cnblogs.com/lisr/p/3925602.html