javascript - 为什么 jslint 在检查代码块内的变量定义时会出错

标签 javascript jquery jslint

我有以下代码:

         $('#modal .update-title')
            .change(function () {
                var title = $('option:selected', this).prop('title');
                $(this).prop('title', title);

                // For the question screen, after the initial set up 
                // changes move the title to the title input field.
                if ($(this).data('propagate-title') === 'yes') {
                    var m = this.id.match(/^modal_TempRowKey_(\d+)$/);
                    if (m) {
                        $("#modal_Title_" + m[1]).val(title);
                    }
                }
            });

当我运行 jslint 时,出现以下错误:

   Combine this with the previous 'var' statement.
   var m = this.id.match(/^modal_TempRowKey_(\d+)$/);

是jslint错了还是我错了?

最佳答案

使用 if 条件不会创建新范围。所以变量 m 仅在条件为真时才存在。所以这就是你可以做的

$('#modal .update-title').change(function () {
    var title = $('option:selected', this).prop('title'),
    m = null; // or just m;
    $(this).prop('title', title);

    // For the question screen, after the initial set up 
    // changes move the title to the title input field.
    if ($(this).data('propagate-title') === 'yes') {
        m = this.id.match(/^modal_TempRowKey_(\d+)$/);
        if (m) {
            $("#modal_Title_" + m[1]).val(title);
        }
    }
});

关于javascript - 为什么 jslint 在检查代码块内的变量定义时会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594291/

相关文章:

javascript - 与该页面内的 iframe 共享页面的全局 javascript 变量

javascript - d3.js 超出最大调用堆栈大小错误

javascript - 需要帮助处理 XML HTTP 文件上传请求

Javascript 获取日期并循环到组合框

javascript - window.location.href = window.location.href 和 JSLint

javascript - 在 Ubuntu 上使用 Node/Npm 安装 JSL 或 JSlint

javascript - 如何在具有相同值的下拉菜单上触发 jQuery 更改事件

javascript - 如何在列表框中显示多个选定的项目

javascript - 下拉菜单不会下拉

javascript - jslint 忽略预期的 '{' 错误