Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Don't make functions within a loop. (https://javascript.ru/forum/misc/45574-don%27t-make-functions-within-loop.html)

barkar 06.03.2014 15:36

Don't make functions within a loop.
 
Есть код:
var ul,x,len....и т.д.
		for(x = 0; x < len; ++x){
			ul.append(
				$('<li></li>', { data: { 'price': jsonres[x].price } }).append(
					$('<img>', { src: jsonres[x].url }),
					$('<p></p>', { text: jsonres[x].name }),
					$('<p></p>', { text: 'Цвет: '+jsonres[x].color }),
					$('<p></p>', { text: 'Размер: '+jsonres[x].size }),
					$('<p></p>', { text: this.digitSpace(jsonres[x].price), data:{'price': jsonres[x].price} }),
					$('<span></span>', {text:'Удалить', click : function(){
							var saleShow = $('#saleShow'),
								prnt = $(this).parent('li'), 
								a = prnt.index(),
								itogo = saleShow.find('li:last>span'),
								result =  itogo.data('itogo') ;
							$.ajax({
								type: 'POST',
								url: '/ajax/ajax.php',
								dataType: 'json',
								data: { deleq: a },
								success: function(delres) {
									prnt.stop(false, true).fadeOut(400, function() {
										sizeSale.text(delres.length);
										var saleShowH = saleShow.outerHeight(true),
											saleShowW = saleShow.outerWidth(true);
										$('#Lcont').css({'width': saleShowW+'px', 'height': saleShowH+'px'});
										result -= prnt.data('price');										
										itogo.data('itogo', result).text( lightbox.digitSpace(result.toFixed(2)) );
										$(this).remove();
										_window_.trigger('resize');	
										if(result === 0){ $('#ext').trigger('click');}
									});									
								}
							});
						}
					})));
			sum += parseFloat(jsonres[x].price);
		}

Выдает ошибку:в JShint выдает ( Don't make functions within a loop. (W083) );
Как поступить?

barkar 06.03.2014 16:26

for(x = 0; x < len; ++x){
            ul.append(
                $('<li></li>', { data: { 'price': jsonres[x].price } }).append(
                    $('<img>', { src: jsonres[x].url }),
                    $('<p></p>', { text: jsonres[x].name }),
                    $('<p></p>', { text: 'Цвет: '+jsonres[x].color }),
                    $('<p></p>', { text: 'Размер: '+jsonres[x].size }),
                    $('<p></p>', { text: this.digitSpace(jsonres[x].price), data:{'price': jsonres[x].price} }),
                    $('<span></span>', {text:'Удалить', click : this.clickDel
                    })));
            sum += parseFloat(jsonres[x].price);
        }

Решил так


Часовой пояс GMT +3, время: 16:09.