简体   繁体   English

JSHint 警告关于使用外部变量的循环内的闭包

[英]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在引用外部作用域变量的循环中声明的函数可能会导致语义混淆

  1. 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。

  2. 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.

相关问题 JSHint 警告:在引用外部范围变量的循环中声明的函数可能会导致语义混淆。 (文件,读者) - JSHint warning: Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. (document, reader) jshint.com 中的 javascript 代码引发警告“在引用外部作用域变量的循环中声明的函数可能会导致语义混淆” - javascript code raising warning in jshint.com "Functions declared within loops referencing an outer scoped variable may lead to confusing semantics" 即使不依赖于词法范围,是否应该使用闭包来构造代码? 如何在循环内重新声明? - Should closures be used to structure code even if they are not depended on lexical scope? How about redeclaration inside loops? 在循环中使用javascript闭包进行上下文 - Using javascript closures for context in loops JsHint警告其他文件中的功能 - JsHint warns about functions inside other files 如何解决jshint警告“循环内声明的函数” - How to resolve jshint warning “Functions declared within loops” sublimeLinter-关于正则表达式字符串中“不必要的转义”的jshint警告 - sublimeLinter - jshint warning about “unnecessary escaping” in regex string JSHint Eclipse插件预定义变量抛出未定义警告 - JSHint Eclipse Plugin Predefined Variables Throwing Not Defined Warning 如何使用闭包在循环中使用then() - How to use then() within loops using closures JSHint引用外部范围 - JSHint referencing an outer scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM