javascript-learner/6.4NFE/t.js
Hanzhang ma c797db0f36 240624
2024-06-24 11:47:26 +02:00

90 lines
1.6 KiB
JavaScript

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){
// 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 (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");
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));