Sam*_*tar 2 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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行 jslint 时,它给了我以下错误:
Combine this with the previous 'var' statement.
var m = this.id.match(/^modal_TempRowKey_(\d+)$/);
Run Code Online (Sandbox Code Playgroud)
是 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);
}
}
});
Run Code Online (Sandbox Code Playgroud)