[英]JSHint warning about closures inside loops using outer variables
My code does work but I don't want the jshint errors anymore:我的代码确实有效,但我不想再出现 jshint 错误:
Functions declared within loop referencing an outer scoped variable may lead to confusing semantics在引用外部作用域变量的循环中声明的函数可能会导致语义混淆
I've tried using let from ES6 to get around the error because I thought that would solve the problem.我尝试使用 ES6 中的 let 来解决错误,因为我认为这可以解决问题。 I configured my gruntfile to use ES6 as well.我也将 gruntfile 配置为使用 ES6。
I tried using two loops, the outer loop with variable 'i' and the inner loop with variable 'j'我尝试使用两个循环,带有变量“i”的外循环和带有变量“j”的内循环
Neither worked.都没有工作。
Full code provided here: https://jsfiddle.net/rwschmitz/zz7ot3uu/此处提供完整代码: https : //jsfiddle.net/rwschmitz/zz7ot3uu/
var hobbies = document.getElementsByClassName("hobbies");
var active = false;
// For mouse input
for (var i = 0; i < 5; i++) {
hobbies[i].onmouseover = function() {
hobbies[0].classList.add('hobbies-slide-left');
hobbies[1].classList.add('hobbies-slide-right');
hobbies[2].classList.add('hobbies-slide-left');
hobbies[3].classList.add('hobbies-slide-right');
hobbies[4].classList.add('hobbies-slide-left');
};
}
// For click input
for (var i = 0; i < 5; i++) {
hobbies[i].onclick = function() {
hobbies[0].classList.add('hobbies-slide-left');
hobbies[1].classList.add('hobbies-slide-right');
hobbies[2].classList.add('hobbies-slide-left');
hobbies[3].classList.add('hobbies-slide-right');
hobbies[4].classList.add('hobbies-slide-left');
};
}
You could change your loops to something like this, using Array#forEach()
:您可以使用Array#forEach()
将循环更改为类似的内容:
var hobbies = Array.from(document.getElementsByClassName('hobbies'));
var classes = ['hobbies-slide-left', 'hobbies-slide-right'];
var events = ['mouseover', 'click'];
function addHobbyClass (hobby, index) {
hobby.classList.add(this[index % this.length]);
}
function hobbyEventListener () {
hobbies.forEach(addHobbyClass, classes);
}
hobbies.forEach(function (hobby) {
this.forEach(function (event) {
this.addEventListener(event, hobbyEventListener);
}, hobby);
}, events);
Two additional examples of how to fix the problem.关于如何解决问题的另外两个示例。
var hobbies = document.querySelectorAll('.hobbies'); var eventHooks = ['mouseover', 'click']; hobbies.forEach(function(hobby) { eventHooks.forEach(function(hook) { hobby.addEventListener(hook, function() { hobbies[0].classList.add('hobbies-slide-left'); hobbies[1].classList.add('hobbies-slide-right'); hobbies[2].classList.add('hobbies-slide-left'); hobbies[3].classList.add('hobbies-slide-right'); hobbies[4].classList.add('hobbies-slide-left'); }); }); });
var hobbies = document.getElementsByClassName('hobbies'); var eventHooks = ['mouseover', 'click']; // Attach events var attachEvents = function(key) { eventHooks.forEach(function(hook) { hobbies[key].addEventListener(hook, function() { hobbies[0].classList.add('hobbies-slide-left'); hobbies[1].classList.add('hobbies-slide-right'); hobbies[2].classList.add('hobbies-slide-left'); hobbies[3].classList.add('hobbies-slide-right'); hobbies[4].classList.add('hobbies-slide-left'); }); }); }; // Init var init = function() { // Loop through hobbies for (var i = 0; i < hobbies.length; i++) { attachEvents(i); } } init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.