<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="btn">CLICK</button>
</ul>
</body>
</html>
<script>
document.getElementById('btn').addEventListener('click',function(){
function counter(){
var count = 0;
function innerCounter(){
return ++count;
}
return innerCounter;
}
var countFunc = counter();
console.log( countFunc() ); // 1
console.log( countFunc() ); // 2
console.log( countFunc() ); // 3
})
</script>
我溪網每次點擊後console.log是會紀錄上一次累加的值
例如:第一次點擊 1 2 3
第二次點擊 4 5 6
let count = 0;
document.getElementById('btn').addEventListener('click', () => {
console.log(++count);
console.log(++count);
console.log(++count);
});