(1) const firstArr = new Array();
(2) const secondArr = Array();
(3) const thirdArr = [];
(1) const firstArrEle = new Array(8)
console.log(firstArrEle); // [empty × 8] or [ <8 empty items> ]
즉, [ , , , , , , , ] 이런 모습으로 8개의 요소가 빈상태로 빈 배열을 만들 수 있다.
(2) const secondArrEle = Array(8);
위와 같은 결과이다.
(1) const forthArrFill = new Array(4).fill('item')
console.log(forthArrFill); // [ 'item', 'item', 'item', 'item' ]
(2) const fifthArrFill = Array(4).fill('item')
console.log(fifthArrFill); // [ 'item', 'item', 'item', 'item' ]
- 배열의 prototype으로 foo라는 함수를 추가함
Array.prototype.foo = function() { return 100; }
- 배열 colors 선언
const colors = ['red', 'green', 'blue']
for(const color of colors) {
console.log(color);
}
결과
'red'
'green'
'blue'
For...of은
(1) 반복 할 수 있는 것들만 반복한다.
(2) 순서를 나열 할 수 있는 것들만 반복한다.
(3) prototype chain에 영향을 받지 않는다.
for(const color in colors) {
console.log(color);
}
결과
'0'
'1'
'2'
'foo'
for(const color in colors) {
console.log(color, colors[color]);
}
결과
'0' 'red'
'1' 'green'
'2' 'blue'
'foo' [Function]
for...in은
(1) 반복할 수 없는 것도 반복한다.
(2) 반복 시 index가 담겨있다.
(3) prototype chain에 영향을 받는다.