Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在循环中定义函数的最佳方法是什么JavaScript_Javascript_Jquery_Loops - Fatal编程技术网

在循环中定义函数的最佳方法是什么JavaScript

在循环中定义函数的最佳方法是什么JavaScript,javascript,jquery,loops,Javascript,Jquery,Loops,我想知道在循环中定义函数的哪种方法更好?我总是用第一种方法。对吗?还是有别的办法 请引导我,我在代码中经常使用第一种方式,我想知道这是不是真的 $('.elements').each(function(){ // elements var $t = $(this), $anotherEl = $t.find('.el2'), $anotherEl2 = $t.find('.el3'); // variables var isVisble = $t.is(':visible'),

我想知道在循环中定义函数的哪种方法更好?我总是用第一种方法。对吗?还是有别的办法

请引导我,我在代码中经常使用第一种方式,我想知道这是不是真的

$('.elements').each(function(){
  // elements
  var $t = $(this), $anotherEl = $t.find('.el2'), $anotherEl2 = $t.find('.el3');

  // variables
  var isVisble = $t.is(':visible'), isLand = $t.hasClass('.land');

  function doSomething(){
    $t.width($anotherEl.width() + $anotherEl2.width());
    // other codes ...
  }
  doSomething();
  $(window).resize(doSomething);
});


感谢您的帮助。

为什么要在循环中执行此操作?因为这可以在不使用以下循环的情况下完成:

function doSomething(){
    $('.elements').each(function(){
         // elements
         var $t = $(this), $anotherEl = $t.find('.el2'), $anotherEl2 = $t.find('.el3');
         // variables
         var isVisble = $t.is(':visible'), isLand = $t.hasClass('.land');
         // now do something with the vars here.
    });
}

$(window).resize(doSomething).resize(); // <---trailing .resize() triggers on dom ready.
函数doSomething(){
$('.elements')。每个(函数(){
//元素
var$t=$(this),$anotherEl=$t.find('.el2'),$anotherEl2=$t.find('.el3');
//变数
变量isvisable=$t.is(':visible'),isLand=$t.hasClass('.land');
//现在对这里的VAR做点什么。
});
}

$(窗口).resize(doSomething).resize();// 为什么在循环中这样做?因为这可以在不使用以下循环的情况下完成:

function doSomething(){
    $('.elements').each(function(){
         // elements
         var $t = $(this), $anotherEl = $t.find('.el2'), $anotherEl2 = $t.find('.el3');
         // variables
         var isVisble = $t.is(':visible'), isLand = $t.hasClass('.land');
         // now do something with the vars here.
    });
}

$(window).resize(doSomething).resize(); // <---trailing .resize() triggers on dom ready.
函数doSomething(){
$('.elements')。每个(函数(){
//元素
var$t=$(this),$anotherEl=$t.find('.el2'),$anotherEl2=$t.find('.el3');
//变数
变量isvisable=$t.is(':visible'),isLand=$t.hasClass('.land');
//现在对这里的VAR做点什么。
});
}

$(窗口).resize(doSomething).resize();//这是一个范围问题。我建议使用第二种方法。动态函数很难调试。您可以根据参数决定执行什么。这样,它将是可伸缩和可维护的。这也会给你定制处理的空间。是的,你是对的,拉杰什,但我认为第一种方法更容易!在一个循环中,基本上是很多函数对一个函数。。。因此,您还可以通过在RAM中不存储太多相同内容来获得性能。第二种方法。这是一个范围问题。我建议使用第二种方法。动态函数很难调试。您可以根据参数决定执行什么。这样,它将是可伸缩和可维护的。这也会给你定制处理的空间。是的,你是对的,拉杰什,但我认为第一种方法更容易!在一个循环中,基本上是很多函数对一个函数。。。因此,您还可以通过在RAM中不存储太多相同内容来获得性能。第二种方法。因为定义的变量不需要更改。@AliAmini嗯,我会按照我发布的那样做,因为这对于使用
是很好的。每个(
循环)但是resize事件必须在该循环之外,否则将为循环中的每个元素注册resize事件。我必须注册
doSomething()
调整大小事件的功能。我编辑了我的帖子,并对代码做了一些修改。请看。谢谢。@AliAmini,但在我看来,这有点多余,因为变量在每个循环的两个不同作用域中声明,在函数中声明。这是可以优化的。因为定义的变量不需要更改。@AliAmini好吧,我会按照我发布的那样做,因为这对于使用
很好。each()
循环但是resize事件必须在该循环之外,否则将为循环中的每个元素注册resize事件。我必须注册
doSomething()
调整大小事件的功能。我编辑了我的帖子,并对代码做了一些修改。请看。谢谢。@AliAmini,但在我看来,这有点多余,因为变量在每个循环的两个不同作用域中声明,在函数中声明。这可能是一种优化。