2
我有以下代碼:檢查代碼塊中的變量定義時,爲什麼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的錯還是我我錯了?
塊* *不*在JavaScript中引入一個新的作用域,內部的'var'被「懸掛」。 JSLint告訴你某人(例如Crockford)認爲所有局部變量聲明都應該放在* single *'var'語句中的函數塊的*頂部。 (我不同意任何「風格」指南,也不支持JSLint - 也許可以根據您的指導方針/風格讓JSLint驗證*有效*代碼的選項) – 2012-09-26 03:55:34
(JSLint是*自定義軟件*。將會在一些完全有效的語法結構上出現「錯誤」。使用該工具時請注意:它是*您的*代碼。) – 2012-09-26 04:02:18
@pst - 感謝您的評論。你提到這些塊不會引入新的範圍。這是否適用於在函數中聲明的變量?如果我有一個帶有函數的.js文件,那麼其中的幾個函數是每個變量聲明都被提升到頂部? –