본문 바로가기

Web Dev/Javascript

Javascript 문법 - 구조분해할당(Destructing)

반응형

구조분해할당 = 비구조화할당

const child = {
    name: "준호",
    age: 30,
    school: "USC",
    createdAt: "2021-03-15",
}


// const name = "joonho"
// const age = 30
// const school = "서라벌초등학교"
// const createdAt = "2020-01-01"

const { name, age, school, createdAt } = child

getWelcomeTemplate({ name, age, school });
function getChild() {
    return { namec: "철수", agec: 13, schoolc: "다람쥐초등학교", }
}

const { schoolc } = getChild();
console.log('schoolcs', schoolc)
const [child1 ,child2] =["철수" , "영희"]
function getClassmates(aaa,bbb)
{
return [bbb,aaa]
}
const [c1, c2] = getClassmates("맹구","훈이")

요즘에는 이렇게 많이 쓴다 


function getWelcomeTemplate({ name, age, school, createdAt }) {
    const result = `
    <html>
        <body>
            <h1>철수님 가입을 환영합니다!!</h1>
            <hr/>
            <div> 이름 : ${name} </div> 
            <div> 나이 : ${age} </div>
            <div> 학교 : ${school} </div>
            <div> 가입일 : ${createdAt} </div>
        </body>
    </html>
    `
    console.log('result', result)
}
const child = {
    name: "준호",
    age: 31,
    school: "건국대학교",
    createdAt: "2021-03-15",
}

const { name, age, school, createdAt } = child

getWelcomeTemplate({ name, age, school, createdAt });
반응형