GithubHelp home page GithubHelp logo

Comments (7)

Genzhen avatar Genzhen commented on May 1, 2024 13

答案
0 1 2

解析
使用let关键字声明变量i:使用let(和const)关键字声明的变量是具有块作用域的(块是{}之间的任何东西)。 在每次迭代期间,i将被创建为一个新值,并且每个值都会存在于循环内的块级作用域。

// 下面代码输出什么
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

答案:3 3 3,由于JavaScript中的事件执行机制,setTimeout函数真正被执行时,循环已经走完。 由于第一个循环中的变量i是使用var关键字声明的,因此该值是全局的。 在循环期间,我们每次使用一元运算符++都会将i的值增加1。 因此在第一个例子中,当调用setTimeout函数时,i已经被赋值为3。

from fe-interview.

webyangpei avatar webyangpei commented on May 1, 2024 3

答案
0 1 2

解析
使用let关键字声明变量i:使用let(和const)关键字声明的变量是具有块作用域的(块是{}之间的任何东西)。 在每次迭代期间,i将被创建为一个新值,并且每个值都会存在于循环内的块级作用域。

// 下面代码输出什么
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

答案:3 3 3,由于JavaScript中的事件执行机制,setTimeout函数真正被执行时,循环已经走完。 由于第一个循环中的变量i是使用var关键字声明的,因此该值是全局的。 在循环期间,我们每次使用一元运算符++都会将i的值增加1。 因此在第一个例子中,当调用setTimeout函数时,i已经被赋值为3。

for(var i = 0; i < 3; i++) { setTimeout(console.log(i)) } 这个结果是 0 1 2

from fe-interview.

sweetliquid avatar sweetliquid commented on May 1, 2024

意义不大

from fe-interview.

Genzhen avatar Genzhen commented on May 1, 2024

意义不大
@lgwebdream 感谢发表自己的见解

理论的储备还是需要的,不一定每个人都知道这点知识,像在没有 let 之前 这个for 循环还是经常出现在面试题中的

from fe-interview.

shiaofang avatar shiaofang commented on May 1, 2024

学到了~谢谢

from fe-interview.

EaVanCN avatar EaVanCN commented on May 1, 2024

也可以用IIFE创建一个新的作用域
for (var i = 0; i < 3; i++) {
(function(){
var j = i;
setTimeout(() => console.log(j), 1);
})()
}
//0 , 1 , 2

from fe-interview.

SnailOwO avatar SnailOwO commented on May 1, 2024

感觉这题应该改成,有几种方式让i 在这题的基础上输出0,1,2。这样会更好

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.