Aller au contenu principal

📄 Counter

Question Description

建立一個計數器,允許透過 add()minus() 方法來增加或減少計數器的值。

1. JavaScript Version

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter</title>
</head>
<body>
<p>Count: <span id="count">0</span></p>
<button id="addBtn">Add</button>
<button id="minusBtn">Minus</button>

<script src="counter.js"></script>
</body>
</html>

const createCounter = () => {
let count = 0;
const $countEl = document.querySelector('#count');

const add = () => {
count++;
$countEl.textContent = count;
};

const minus = () => {
count--;
$countEl.textContent = count;
};

return { add, minus };
};

const counter = createCounter();

document.querySelector('#addBtn').addEventListener('click', counter.add);
document.querySelector('#minusBtn').addEventListener('click', counter.minus);

2. TypeScript Version

使用工廠函式 createCounter 來建立計數器物件,將 add()minus() 方法回傳,並將 count 變數設定為私有變數,避免外部直接存取。

type CounterType = {
add: () => void;
minus: () => void;
};

const createCounter = (): CounterType => {
let count: number = 0;

const add = () => {
count++;
console.log(count);
};

const minus = () => {
count--;
console.log(count);
};

return { add, minus };
};

const counter = createCounter();
counter.add(); // 1
counter.add(); // 2
counter.minus(); // 1