JavaScript | 객체

DDOL9·2024년 4월 1일

JavaScript

목록 보기
1/3

코드잇 스프린트 Pre-Course : JavaScript 핵심 정리

객체 (Object)

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 (변수 in 객체) { 동작 } 의 형태로 작성

for (key in student) {
	console.log(key);
    console.log(student[key])
}

profile
난.. ㄱㅏ끔.. 알코올을 흘린ㄷr..

0개의 댓글