JS 闭包得到一个递增的值

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button id="mybtn" >点击</button>
</body>

<script>

function getNumFunction(){
var a=0;
return function(){

a+=1;
console.log(a);

}
}
var fun= getNumFunction();
document.getElementById('mybtn').onclick = function(){
fun();
}

</script>


</html>

 

 

posted @ 2020-04-28 20:33  锋_锋  阅读(449)  评论(0编辑  收藏  举报