javascript - 避免警告 "Use of this in closure"

标签 javascript jquery

当我编写这段代码时,我有两个警告“在闭包中使用此内容”

 function tableIntoJson(){
    var tableArray = [];
    var columnIndex=0;
    var rowIndex=0;
    $('table#editable tr:not(#head)').each(function() {
        var tableData = $(this).find('td:not(#head)');
        tableArray[rowIndex]=[];
        if (tableData.length > 0) {
            columnIndex=0;
            tableData.each(function() {
                tableArray[rowIndex][columnIndex]=$(this).text();
                columnIndex++
            });
        }
        rowIndex++;
    });
    var jsonString=JSON.stringify(tableArray);
    return jsonString;
}

警告让我烦恼))。我如何替换“$(this)”?

最佳答案

即使这是 this 的有效使用,但如果您想让您的 IDE 满意,请替换

$('table#editable tr:not(#head)').each(function() {
    var tableData = $(this).find('td:not(#head)');

$('table#editable tr:not(#head)').each(function(trIndex, tr) {
    var tableData = $(tr).find('td:not(#head)');

(下一个 .each 类似)。

它也比使用 this 更具可读性,因为通过这种方式,您可以明确地知道每个循环迭代的内容。

关于javascript - 避免警告 "Use of this in closure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124528/

相关文章:

javascript - 按钮单击事件仅在页面重新加载或功能提醒时起作用

javascript - 对象中的最高值,用键显示,javascript

javascript - 尝试动态设置 MetaTag

jquery-ui-ressized-alsoresize-reverse — 如何让两个相邻的 div 从相邻边缘向同一方向调整大小

javascript - 使div在单击时上下移动

javascript - 如何在 vscode 扩展中使用正则表达式保留返回战车?

javascript 从字符串中提取主题标签

c# - data-ajax-update 在 dotnet core jquery unobtrusive ajax 中不起作用

javascript - 显示每个元素的关闭按钮并使用 JavaScript 隐藏当前元素

javascript - 如何创建出现在表格行下方的 div