js 全局、局部变量计数.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>js 全局、局部变量计数</title>
</head>
<body>
<p>全局、局部变量计数。</p>
<button οnclick="count()">计数!</button>
<p id="demo">0</p>
<script>
    let counter = 0;

    function add_global() {
        counter += 1;
        return counter;
    }

    let add_local = (function () {
        let counter = 0;
        return function () {
            return counter += 1;
        }
    })();

    function count() {
        document.getElementById("demo").innerHTML = add_global();
        // document.getElementById("demo").innerHTML = add_local();
    }
</script>
</body>
</html>


发布了197 篇原创文章 · 获赞 61 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/105332542