一个闭包的例子

for( var i = 0; i < 5; i++ ) {
    let temp=j=>{
        setTimeout(() => {
            console.log(j );
        }, 1000 * j)
    }
    temp(i)
}
var x = 15;
function A () {
   console.log(x);
}

function B (fn) {
   var x = 20;
   fn();
}

B(A);  // 15, but not 20

 

posted @ 2020-04-01 19:16  spikespiegel  阅读(124)  评论(0)    收藏  举报