문제

If I want to make an array that is say of length 3 using bracket notation, should I just write: var foo = [,,];

I was more used to writing: var foo = new Array(3);

I noticed that if I removed one , that my code still worked which is surprising because I am accessing all 3 elements after assigning them. How is it that it would still work?

도움이 되었습니까?

해결책

if u do

a = []
a[3] = 100

the indices 0,1,2 will be filled in with undefined for u. u do not have to set a specific array length before using certain indices. array will grow as u use it.

all these are the same:

a = [,,,]

b = []
b.length = 3

c = new Array(3)

d = []
d[2] = undefined
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top