2024-06-23 23:57:39 +02:00
|
|
|
function sayHi(){
|
|
|
|
console.log("Hi");
|
|
|
|
|
|
|
|
sayHi.counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(sayHi.name)
|
|
|
|
|
|
|
|
let user = {
|
|
|
|
sayHi(){
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
sayBye: function (){
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(user.sayBye.name)
|
|
|
|
console.log(user.sayHi.name)
|
|
|
|
|
|
|
|
function ask(question, ...handlers){
|
2024-06-24 11:47:26 +02:00
|
|
|
// let isYes = confirm(question);
|
|
|
|
let isYes;
|
|
|
|
let decision = Math.floor(Math.random() * 2);
|
|
|
|
isYes = decision !== 0;
|
|
|
|
console.log(`isYes? ${isYes}`);
|
2024-06-23 23:57:39 +02:00
|
|
|
|
|
|
|
for(let handler of handlers){
|
2024-06-24 11:47:26 +02:00
|
|
|
if (handler.length === 0){
|
2024-06-23 23:57:39 +02:00
|
|
|
if (isYes) handler();
|
|
|
|
}else{
|
|
|
|
handler(isYes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2024-06-24 11:47:26 +02:00
|
|
|
sayHi = function func(who){
|
2024-06-23 23:57:39 +02:00
|
|
|
console.log(`Hello, ${who}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
sayHi("John");
|
|
|
|
|
2024-06-24 11:47:26 +02:00
|
|
|
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));
|
2024-06-23 23:57:39 +02:00
|
|
|
|