search

Home  >  Q&A  >  body text

javascript - js let and var issues

Why cannot parameters be passed in using global variables (var i = 0), but can be passed in with regional variables (let i = 0)

        for (var i = 0; i < inputs.length; i++) {
            inputs[i].onfocus = function(){
                showTip(i);
            };
        };
        function showTip(n){
            tips[n].style.color = "red";
        }
仅有的幸福仅有的幸福2797 days ago856

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-06-26 10:54:34

    For Nikkei questions, please search for: Closures, JavaScript block-level scope

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-26 10:54:34

    let is only valid in the current block-level scope (within the braces of for), and each loop can be regarded as a new variable.
    You can refer to the let command in Ruan Yifeng’s Getting Started with ECMAScript 6

    reply
    0
  • Cancelreply