for … in
- 객체의
Enumerable
속성이 true
인 프로퍼티들의 key
값을 순회할 수 있게 해주는 반복문
- 객체를 순회할 때 일관된 순서를 보장하지 않는다
for ... in
반복문 구현 방법에 따라 순회 순서가 다르기 때문에
- 따라서 객체의 속성을 확인하는 디버깅 용도로 사용하는 것이 좋다.
for … of
iterable
한 객체, 즉 Symbol.iterator
메서드를 갖고 있는 컬렉션 요소들에서 사용 가능한 반복문
for … in과 for … of의 차이점
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3,5,7];
iterable.foo = "Hello";
for(let i in iterable) {
console.log(i);
}
// 0, 1, 2, "foo", "arrCustom", "objCustom"
for(let i of iterable){
console.log(i);
}
// 3, 5, 7