以使其與頂部:
var a = new Array();
// 'a' is now an empty array, plain ol' boring empty array, could have been
// written as a = [];
var b;
// 'b' have no value and is 'undefined'. 'console.log(b); // undefined'
a[b++] = 1;
// Lets break the above statement down into pieces:
b++ // Increment b by one, undefined + 1 === NaN
a[ ] // Use NaN as a property for our array 'a'
= 1; // Assign 1 to that property
// Ok? So what happened why does 'a' still look empty?
console.log(a); // []
// The way your console with show you an array is by printing the numeric keys
// in it, and looking at the length, eg:
// var q = [];
// q.length = 1;
// console.log(q); // [undefined x 1]
// With 'a' in our case there is no numeric keys in it so [] is printed.
// So did our value dissapear?
// No. It is there:
console.log('NaN' in a); // true
// And:
for (var prop in a) console.log(prop); // NaN
// Why is this even a feature?
// Arrays are extending Objects so they have the same properties as em.
console.log(a instanceof Object); // true
console.log(a instanceof Array); // true
它確實 - 它分配了'1'到'一個[NaN的]'。嘗試訪問'一個[NaN]'看看它在那裏。這也是一條很滑的道路 - 你期望從一個不標準化的主機對象(「控制檯」不是)。那麼,你怎麼知道你看到的是*預期*還是沒有? – zerkms