add 6.2 and 6.3
This commit is contained in:
		
							
								
								
									
										39
									
								
								6.3variablescope/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								6.3variablescope/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
|  | ||||
| function sum(a){ | ||||
|     return function(b){ | ||||
|         return a + b; | ||||
|     } | ||||
| } | ||||
|  | ||||
| console.log(sum(1)(2)); | ||||
| console.log(sum(5)(-1)); | ||||
|  | ||||
|  | ||||
| function byField(Field){ | ||||
|     return (a,b) => a[Field] > b[Field]? 1: -1; | ||||
| } | ||||
|  | ||||
|  | ||||
| function makeArmy() { | ||||
|     let shooters = []; | ||||
|  | ||||
|     let i = 0; | ||||
|     while (i < 10) { | ||||
|         let id = i; | ||||
|         let shooter = function() { // 创建一个 shooter 函数, | ||||
|             console.log( id ); // 应该显示其编号 | ||||
|         }; | ||||
|         shooters.push(shooter); // 将此 shooter 函数添加到数组中 | ||||
|         i++; | ||||
|     } | ||||
|  | ||||
|     // ……返回 shooters 数组 | ||||
|     return shooters; | ||||
| } | ||||
|  | ||||
| let army = makeArmy(); | ||||
|  | ||||
| // ……所有的 shooter 显示的都是 10,而不是它们的编号 0, 1, 2, 3... | ||||
| army[0](); // 编号为 0 的 shooter 显示的是 10 | ||||
| army[1](); // 编号为 1 的 shooter 显示的是 10 | ||||
| army[2](); // 10,其他的也是这样。 | ||||
		Reference in New Issue
	
	Block a user