javascript - js let and var issues
仅有的幸福
仅有的幸福 2017-06-26 10:52:34
0
2
852

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";
        }
仅有的幸福
仅有的幸福

reply all(2)
大家讲道理

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

仅有的幸福

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template