코드잇 스프린트 Pre-Course : JavaScript 핵심 정리
Key : Value 구조로 데이터 저장
let student = {
name : '똘구',
birthday: '1999-01-01',
score: {
subject1: 95,
subject2: 80
}
};
//점 표기법
console.log(student.name)
console.log(student.score.subject1)
//대괄호 표기법
console.log(student[birthday])
//데이터 추가
student.major = 'Material Science Engineering'
//데이터 확인
console.log('age' in student) //False
console.log('name' in student) //True
//데이터 삭제
delete student.score
객체 내부에 프로퍼티 값으로 함수를 지정할 수 있다.
let calculateArea = {
triangle : function (width, height){
return width * height * 0.5
},
rectangle : function (width, height){
return width * height
}
}
console.log(calculateArea.triangle(3, 6))
for (변수 in 객체) { 동작 } 의 형태로 작성
for (key in student) {
console.log(key);
console.log(student[key])
}