펭귄집

배열의 프로퍼티 동적 생성



//배열생성

var arr = ['zero','one','two'];
console.log(arr.length);  //(출력값)  3

//프로퍼티 동적 추가
arr.color = 'blue';
arr.name = 'number_array';
console.log(arr.length);  //(출력값)  3

//배열 원소 추가
arr[3] = 'red';
console.log(arr.length);  //(출력값)  4

//배열 객체 출력
console.dir(arr);



arr배열에 동적으로 color와 name프로퍼티를 추가함
주목할점 - 배열에 동적 프로퍼티가 추가될 경우는 배열의 length가 값이 3으로 바뀌지 않는다는것

그런다음 다시 arr[3]에 원소를 추가하면 length가 4로 바뀜


-> 즉, 배열의 length프로퍼티는 배열 원소의 가장 큰 인덱스가 변했을 경우만 변경