▼ 보완

 

※ 어떠한 매개변수의 값은 변함없이 계속 사용될 때

const test = function(object) {
    //값이 고정된 매개변수 status를 추가할 때 방법
    //과거(1)
    object.status = object.status !== undefined ? object.status : '이상없음'    //object 객체의 status 속성이 undefined가 아니면, object.status를, undefined면 '이상 없음' 이라는 값 지정
    //과거(2)
    object.status = object.status ? object.status : '이상없음'  //object.status가 있으면 object.status를 넣고, 없으면 '이상없음'을 넣어라
    //과거(3)
    object.status =  object.status || '이상없음'        //object.status과 '이상없음' 중 있는 것만 넣어라

    //현대(1)
    object = { status: '이상 없음', ...object }         //...전개연산자 활용 -> status: '이상 없음'을 넣고 나머지 변수들을 넣어라
    //(가장)현대(2)
    fun = function ({ 
        name, 
        age, 
        color, 
        status = '이상 없음' 
    }) {
        return `${name}:  ${age} : ${color} : ${status}`
    }
    return `${object.name}:  ${object.age} : ${object.color}`
}

 

※ ${object.name} 과 같이 해당 속성(매개변수) 호출 시 객체명을 쓰는 번거로움을 없애려면?

→ 객체에서 속성을 꺼내서 쓰자!

+ Recent posts