From c797db0f36db0fa03c3d28435180ab4dd89316b4 Mon Sep 17 00:00:00 2001 From: Hanzhang ma Date: Mon, 24 Jun 2024 11:47:26 +0200 Subject: [PATCH] 240624 --- 6.4NFE/index.html | 10 +++++++ 6.4NFE/t.js | 51 ++++++++++++++++++++++++++++++-- 6.5NewFunctionGrammar/index.html | 10 +++++++ 6.5NewFunctionGrammar/t.js | 7 +++++ 6.6setTimeoutandsetInterval/t.js | 44 +++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 6.4NFE/index.html create mode 100644 6.5NewFunctionGrammar/index.html create mode 100644 6.5NewFunctionGrammar/t.js create mode 100644 6.6setTimeoutandsetInterval/t.js diff --git a/6.4NFE/index.html b/6.4NFE/index.html new file mode 100644 index 0000000..6d72a1b --- /dev/null +++ b/6.4NFE/index.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + diff --git a/6.4NFE/t.js b/6.4NFE/t.js index a4d1392..3e78e7d 100644 --- a/6.4NFE/t.js +++ b/6.4NFE/t.js @@ -20,10 +20,14 @@ console.log(user.sayBye.name) console.log(user.sayHi.name) 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){ - if (handler.length == 0){ + if (handler.length === 0){ if (isYes) handler(); }else{ 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"); -let sayHi = function func(who){ +sayHi = function func(who){ console.log(`Hello, ${who}`); }; 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)); diff --git a/6.5NewFunctionGrammar/index.html b/6.5NewFunctionGrammar/index.html new file mode 100644 index 0000000..ade4b4b --- /dev/null +++ b/6.5NewFunctionGrammar/index.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + diff --git a/6.5NewFunctionGrammar/t.js b/6.5NewFunctionGrammar/t.js new file mode 100644 index 0000000..933d6b5 --- /dev/null +++ b/6.5NewFunctionGrammar/t.js @@ -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 diff --git a/6.6setTimeoutandsetInterval/t.js b/6.6setTimeoutandsetInterval/t.js new file mode 100644 index 0000000..bce1c5f --- /dev/null +++ b/6.6setTimeoutandsetInterval/t.js @@ -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);