簡體   English   中英

javascript 中 [1][1] 和 [1][0] 的結果

[英]Outcome of [1][1] and [1][0] in javascript

我對 javascript 中的結果有疑問,因為我不太明白。 為什么如果我使用此代碼它會得到下一個結果:

var a =[1][1];
var b = [1][0];
if(a){console.log(true);}else{console.log( false);} --> returns false

if(b){console.log(true);}else{console.log(false);} --> returns true

誰能幫我解釋一下 javascript 如何解釋這個結果的確切方式? 非常感謝!

最好的問候,維克多

其實很簡單,讓我們分解一下:

var a =[1][1];

分解為:

var a = [1]; //An array with the value '1' at the 0 index
a = a[1]; //assigns a the result of the 1 index, which is undefined

b相同 - 但b使用0索引,其定義為(如1 );

aundefined是假的, b是 1 - 這是真的。

基本上,您正在使用一個元素為1的數組中的值。

a變得undefined ,因為沒有索引為1元素。
b得到1 ,因為索引0處的元素為1

 var a = [1][1]; // undefined var b = [1][0]; // 1 console.log(a); // undefined console.log(b); // 1 if (a) { console.log(true); } else { console.log(false); // false } if (b) { console.log(true); // true } else { console.log(false); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM