240624
This commit is contained in:
51
6.4NFE/t.js
51
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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user