240624
This commit is contained in:
		
							
								
								
									
										10
									
								
								6.4NFE/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								6.4NFE/index.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | <!DOCTYPE html> | ||||||
|  | <html lang="en"> | ||||||
|  | <head> | ||||||
|  |     <meta charset="UTF-8"> | ||||||
|  |     <title>Title</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  |     <script src="t.js"></script> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										51
									
								
								6.4NFE/t.js
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								6.4NFE/t.js
									
									
									
									
									
								
							| @@ -20,10 +20,14 @@ console.log(user.sayBye.name) | |||||||
| console.log(user.sayHi.name) | console.log(user.sayHi.name) | ||||||
|  |  | ||||||
| function ask(question, ...handlers){ | function ask(question, ...handlers){ | ||||||
|     let isYes = confirm(question); |     // let isYes = confirm(question); | ||||||
|  |     let isYes; | ||||||
|  |     let decision = Math.floor(Math.random() * 2); | ||||||
|  |     isYes = decision !== 0; | ||||||
|  |     console.log(`isYes? ${isYes}`); | ||||||
|  |  | ||||||
|     for(let handler of handlers){ |     for(let handler of handlers){ | ||||||
|         if (handler.length == 0){ |         if (handler.length === 0){ | ||||||
|             if (isYes) handler(); |             if (isYes) handler(); | ||||||
|         }else{ |         }else{ | ||||||
|             handler(isYes); |             handler(isYes); | ||||||
| @@ -35,10 +39,51 @@ ask("Question?", () => console.log('You said yes'), result => console.log(result | |||||||
|  |  | ||||||
| console.log("the difference between the counter.count and closure is the counter.count can be accessed outside but closure cannot"); | console.log("the difference between the counter.count and closure is the counter.count can be accessed outside but closure cannot"); | ||||||
|  |  | ||||||
| let sayHi = function func(who){ | sayHi = function func(who){ | ||||||
|     console.log(`Hello, ${who}`); |     console.log(`Hello, ${who}`); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| sayHi("John"); | sayHi("John"); | ||||||
|  |  | ||||||
|  | function makeCounter(){ | ||||||
|  |     function counter(){ | ||||||
|  |         return counter.count++; | ||||||
|  |     } | ||||||
|  |     counter.set = function setCounter(value){ | ||||||
|  |         counter.count = value; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     counter.decrease = function decreaseCounter(){ | ||||||
|  |         counter.count--; | ||||||
|  |     } | ||||||
|  |     counter.count = 0 | ||||||
|  |     return counter; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | let counter1 = makeCounter(); | ||||||
|  | counter1() | ||||||
|  | console.log(counter1()) | ||||||
|  | counter1.set(45) | ||||||
|  | console.log(counter1.count) | ||||||
|  | counter1.decrease() | ||||||
|  | console.log(counter1.count) | ||||||
|  |  | ||||||
|  | function sum(a){ | ||||||
|  |     let currentSum = a; | ||||||
|  |  | ||||||
|  |     function f(b){ | ||||||
|  |         currentSum += b; | ||||||
|  |         return f | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     f.toString = function(){ | ||||||
|  |         return currentSum; | ||||||
|  |     } | ||||||
|  |     return f; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | console.log(sum(1)(2)); | ||||||
|  | console.log(sum(1)(-2)); | ||||||
|  | alert(sum(1)(2)); | ||||||
|  | alert(sum(1)(-2)); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								6.5NewFunctionGrammar/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								6.5NewFunctionGrammar/index.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | <!DOCTYPE html> | ||||||
|  | <html lang="en"> | ||||||
|  | <head> | ||||||
|  |     <meta charset="UTF-8"> | ||||||
|  |     <title>Title</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  | <script src="t.js"></script> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										7
									
								
								6.5NewFunctionGrammar/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								6.5NewFunctionGrammar/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | let func = new Function('a', 'b', 'return a+b'); | ||||||
|  |  | ||||||
|  | console.log(func(1,2)) | ||||||
|  |  | ||||||
|  | let x = 10; | ||||||
|  | func = new Function('return x;'); | ||||||
|  | console.log(func()); // Error: x is not defined | ||||||
							
								
								
									
										44
									
								
								6.6setTimeoutandsetInterval/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								6.6setTimeoutandsetInterval/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | function sayHi(){ | ||||||
|  |     console.log('Hello'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | setTimeout(sayHi, 1000); | ||||||
|  |  | ||||||
|  | function sayHi2(phrase, who){ | ||||||
|  |     console.log(phrase + ',' + who); | ||||||
|  | } | ||||||
|  | setTimeout(sayHi2, 100, "Hi", "John"); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | let timerId = setInterval(()=>console.log(('tick')), 1000); | ||||||
|  |  | ||||||
|  | setTimeout(() => {clearInterval(timerId); console.log('stop!')}, 5000); | ||||||
|  |  | ||||||
|  | timerId = setTimeout(function tick(){ | ||||||
|  |     console.log('tick'); | ||||||
|  |     timerId = setTimeout(tick, 1000); | ||||||
|  | }, 1000) | ||||||
|  |  | ||||||
|  | function printNumbers(from, to){ | ||||||
|  |     let cur = from | ||||||
|  |  | ||||||
|  |     let timerId = setInterval(() => { | ||||||
|  |         console.log(cur) | ||||||
|  |         if(cur == to){ | ||||||
|  |             clearInterval(timerId); | ||||||
|  |         } | ||||||
|  |         cur += 1; | ||||||
|  |     }, 1000); | ||||||
|  | } | ||||||
|  | function  printNumbers2(from, to){ | ||||||
|  |     cur = from | ||||||
|  |     setTimeout(function go() { | ||||||
|  |         console.log(cur); | ||||||
|  |         if(cur < to){ | ||||||
|  |             setTimeout(go, 1000); | ||||||
|  |         } | ||||||
|  |         cur++; | ||||||
|  |     }, 1000) | ||||||
|  | } | ||||||
|  | printNumbers(10, 15); | ||||||
|  | printNumbers2(10, 15); | ||||||
		Reference in New Issue
	
	Block a user