重複の可能性:
JavaScript の「未定義 x 1」とは?
Chrome 21 では[,]、コンソール出力へのフィード
【未定×1】
および供給[undefined]出力
[未定義]
[undefined]とはどう違い[undefined x 1]ますか?
表記は何[undefined x 1]ですか?
重複の可能性:
JavaScript の「未定義 x 1」とは?
Chrome 21 では[,]、コンソール出力へのフィード
【未定×1】
および供給[undefined]出力
[未定義]
[undefined]とはどう違い[undefined x 1]ますか?
表記は何[undefined x 1]ですか?
[,]長さが1で、インデックスがない配列を作成します。
[undefined]undefinedインデックスに値を持つ長さ1の配列を作成します0。
Chromeundefined × xは、シーケンシャルインデックスを持たないスパース配列用です。
var a = [];
a[8] = void 0; //set the 8th index to undefined, this will make the array's length to be 9 as specified. The array is sparse
console.log(a) //Logs undefined × 8, undefined, which means there are 8 missing indices and then a value `undefined`
スパース配列で使用する場合は.forEach、存在しないインデックスをスキップします。
a.forEach(function() {
console.log(true); //Only logs one true even though the array's length is 9
});
.length通常ベースのループを実行する場合:
for (var i = 0; i < a.length; ++i) {
console.log(true); //will of course log 9 times because it's only .length based
}
.forEach非標準の実装と同じように動作することを期待する場合は、落とし穴があります。
new Array(50).forEach( function() {
//Not called, the array doesn't have any indices
});
$.each( new Array(50), function() {
//Typical custom implementation calls this 50 times
});
これは、Chrome 21 の私にとっては奇妙な[]出力です。[]
とにかく[a, b, c, ...]Javascriptの配列表記なので、基本的に値のない配列を定義しています。
,ただし、配列の生成を容易にするためのエンディングは許容されます。Chrome が伝えているのは、配列に未定義の値が 1 つあるということです。例については、コードを参照してください。
[,,]
> [undefined x2]
[1,2,]
> [1, 2]
[1,2,,]
> [1, 2, undefined × 1]
[1,2,,,]
> [1, 2, undefined × 2]
[1,2,,3,4,,,6]
> [1, 2, undefined × 1, 3, 4, undefined × 2, 6]
繰り返される「未定義」の値を表示する簡単な方法のようです。例えば:
> [,,,]
[ undefined x 3 ]
しかし、まったく[]同じではありません[undefined]。私があなただったら、それを再確認します。