jslint-不要在循环内创建函数 [英] jslint - Don't make functions within a loop

查看:69
本文介绍了jslint-不要在循环内创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅本主题:不要在循环中创建函数. -jslint错误

您将如何在for循环中处理jquery .each(function(){...}?函数的参数是在循环外部声明的函数,但是从我的角度来看,它会影响可读性.

How would you handle a jquery .each(function () {...} within a for loop? knowing that i need the context of the "for" in my "each" function. Sure I could map every required to parameters to a function a function declared outside of the loop but from my perspective, it impacts the readability.

有什么想法吗?

谢谢.

推荐答案

好吧,您可以将for的上下文保留在循环中,因为for中的所有内容实际上都与在开始时声明的函数处于同一上下文中.

Well, you can keep the context of the for in your loop, as everything in the for is actually in the same context as a function declared at the start.

所以让我们以Frits为例,但是首先让我们使这个完全JSLint满意(减去循环错误中调用的函数).

So let's take Frits' example, but first let's make this fully JSLint happy (minus the function called in the loop error) first.

/*global console*/
var my_func, i, list;
for (i = 0; i < list.length; i+= 1) {
    my_func = function (i) {
        console.log(i);
    };
    my_func(i);
}

请注意,每次 您都要循环访问循环,而您要重新声明 my_func函数.那不酷!为什么要一遍又一遍地重新声明相同的功能?

Note that each time you iterate the loop, you're redeclaring the my_func function. That's not cool! Why re-declare the same function over and over?

像这样早点声明:

/*global console*/
var my_func, i, list;

my_func = function (i) {
    console.log(i);
};

for (i = 0; i < list.length; i+= 1) {
    my_func(i);
}

成功.现在,您不必每次迭代都创建一个函数.而且,由于JSLint通过将所有var声明推到顶部来帮助您实现,所以您仍然具有相同的上下文.

Success. Now you don't create a function with each iteration. And, as JSLint helps you realize by pushing all your var declarations to the top, you still get to have the same context.

编辑:作为

As @Flame points out, you don't have to declare the function early with jQuery each and can use an anonymous function, but it's not a bad idea to declare early, especially if you're doing to reuse the logic in multiple each calls. The main take-homes are to understand that 1.) The early declaration practice still has advantages, and 2.) jQuery's still going to send along arguments to your function (here, what we're calling index), though JSLint will not (and should not) complain about anonymous functions used in eachs (jQuery sauce here).

如果您习惯使用匿名$.each(function() {});构造,一开始会更加不直观,但是它同样简单.

It is initially a little more unintuitive if you're used to the anonymous $.each(function() {}); construct, but it's just as easy.

/*global alert, $ */
$( "li" ).each(function( index ) {
    alert( index + ": " + $(this).text() );
});

...变成...

/*global $, alert */
var fnOutside = function(index) {
    alert( index + ": " + $(this).text() );
};
$( "li" ).each(fnOutside);

这可能会造成短暂的混乱,因为该函数调用没有参数,但是jQuery是将"index"参数推向该函数的原因.如果您想省略命名,则可以获取上下文arguments数组以查看它是否仍在其中.

That might be confusing briefly because the function call doesn't have parameters, but jQuery is what's pushing the "index" parameter to the function. You could just grab the contextual arguments array to see that it's still pushed in there if you wanted to leave the naming out.

小提琴

也就是说,for构造不会创建新的闭包.那可能会让您担心.没问题!

That is to say, the for construct doesn't create a new closure. That might be worrying you. It's no problem!

这篇关于jslint-不要在循环内创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆