#2_object 및 parameter

유상우·2023년 6월 5일
0

Nest.js 프로젝트

목록 보기
2/7

1. 여러 객체 간단히 표현(shorthand property)

프로그래밍을 하다 보면 코드를 보다 짧게 작성함으로써 기능을 해치진 않지만, 좀 더 깔끔해보이고 유지 보수에도 좋은 형태로 작업을 할 수 있다.

다음 세 가지 profile은 형태는 다르지만 기능은 같다.

# profile 1

const profile1 = {
     name: 'happy',
     age: 12,
     school: 'earch'
}


# profile 2
const name = 'happy'
const age = 12
const school = 'earch'

const profile2 = {
     name: name,
     age: age,
     school: school
}


# profile 3

const name = 'happy'
const age = 12
const school = 'earch'

const profile3 = {
     name,
     age,
     school
}

2. 구조 분해 할당

객체에 들어있는 key,value 값을 새로운 변수 값으로 할당 할 때 주로 쓰인다. 배열에서도 똑같이 쓰일 수 있다.

# 1. object

const profile = {
     name: 'happy',
     age: 12,
     school: 'earch'
}

const {name, age, school} = profile



# 2. array

const profile = ['happy', 12, 'earth']

const [name, age, school] = profile

하나의 객체를 parameter로 받을 때, 필요한 객체의 key와 value 값만 받을 수도 있다.

parameter로 데이터를 받을 때 각각의 데이터를 순서와 관계 없이 받을 수 있으므로 객체 형태로 받는 것이 낫다.

function profileFunction({school, age}) {
     console.log(age, school);
}

profile = {
     name: 'happy',
     age: 12,
     school: 'earth'
}

profileFunction(profile)
profile
Potentialist

0개의 댓글

관련 채용 정보