3-6、箭头函数的应用

52 阅读1分钟
<script>
    // const btn = document.getElementById('btn');
    // const result = document.getElementById('result');

    // const timer = {
    //     time: 0,
    //     start: function () {
    //         // this
    //         var that = this;
    //         btn.addEventListener('click', function () {
    //             setInterval(function () {
    //                 // this.time++;
    //                 // result.innerHTML = this.time
    //                 that.time++;
    //                 result.innerHTML = that.time
    //             }, 1000)
    //         })
    //     }
    // }


    // const timer = {
    //     time: 0,
    //     start: function () {
    //         // this
    //         btn.addEventListener('click', () => {
    //             // this
    //             setInterval(() => {
    //                 console.log(this);
    //                 this.time++;
    //                 result.innerHTML = this.time
    //             }, 1000)
    //         })
    //     }
    // }
    // timer.start();
</script>