JSHint / JSLintのすべてのエラーは、キー/イテレータ変数を宣言しなかったことを意味します。@Christopherが示唆しているように、JSLintは、スコープの上部で宣言することを望んでいます(このリンクのようJavaScript hoisting
に、巻き上げの詳細についてはgoogleを参照してください)。
/*global data, identifier, DIVsuggestions */
// We'll pretend all of the above were passed in from a function's parameters
// by using JSLint's "global" keyword -- now you can paste this code into
// jslint.com and have it pass muster.
// make array
var sugested_sports = data.split(","),
sporty_items = '', // pre build DIV
sport; // <<<< **** DECLARE YOUR "KEY" HERE ****
for (sport in sugested_sports)
{
if (sugested_sports.hasOwnProperty(sport)) {
sporty_items += '<a href="#'+identifier[1]+'">'
+sugested_sports[sport]+'</a>';
}
}
// insert DIV
DIVsuggestions.html(sporty_items);
ここでのこのbad for in variable
エラーは、他の場所でのエラーと同じになり'sport' was used before it was defined
ます。
編集:for
あなたが内部関数にいる場合はfor in
、同じコンテキストで変数を宣言する必要があることに言及する価値があります。for in
親コンテキストでを宣言すると、JSLintは文句を言います。
例:
function spam(d)
{
var fnTest, row; // `row` is defined "too early"
fnTest = function (data) {
for (row in data)
{
if (data.hasOwnProperty(row))
{
console.log(data.row);
}
}
};
fnTest(d);
}
物事を幸せにするためrow
に、内部機能に移動します。JSLintは、技術的にはまだ範囲内にありますが、以前に使用されていた「スーパースコープ」は好きではありません。
function spam(d)
{
var fnTest;
fnTest = function (data) {
var row; // and JSLint is happy! ;^D
for (row in data)
{
if (data.hasOwnProperty(row))
{
console.log(data.row);
}
}
};
fnTest(d);
}
ちなみに、ジェームズの懸念は
hasOwnProperty
、OPが挿入したチェックでカバーされています。そのチェックを外すと、JSLintは「プロトタイプから不要なプロパティをフィルタリングするために、forinの本体をifステートメントでラップする必要があります」と文句を言います。
興味があれば、hasOwnPropertyとfor...inについてもう少し詳しく説明します。