hasOwnProperty
-
[javascript] hasOwnProperty를 통해 객체안에 속성여부 확인Front-end/Javascript 2020. 5. 26. 14:18
EX-1) var store = {snack:1000, flower:5000, beverage:2000}; for (var item in store){ if(!store.hasOwnProperty(item)) continue; console.log(item + ' 는 가격이'+store[item]+' 입니다.') } 위 코드는 store 객체를 순환하는 for - in 반복문 이다. 매 반복마다 hasOwnProperty를 이용하여 store 객체에 item 키 정보가 있는지 확인한다. 없으면 continue(계속진행) 를 통해 아래 코드는 실행하지않고 다음 순서로 넘어간다. 결과값 snack 는 가격이1000 입니다. flower 는 가격이5000 입니다. beverage 는 가격이2000 입니다. *..