我有以下代码:
$('#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/