비구조화 할당

릭터·2026년 1월 26일

Javascript

목록 보기
5/8

비구조화 할당

객체와 배열로 부터 속성이나 요소를 쉽게 꺼낼 수 있다.

객체

var candyMachine = {
	status: {
    	name: 'node',
        count:5,
     },
     getCandy: function(){
     	this.status.count--;
        return this.status.count;
     }
};
var getCandy = candymachine.getCandy;
var count = candyMachine.statue.count;

위의 코드를 밑의 코드로 바꿀 수 있다.

const candyMachine = {
	statue: {
    	name: 'node',
        count:5,
    },
    getCandy(){
    	this.status.count--;
        return this.status.count;
        }
 };
 const {getCandy, status: {count} } = cndyMachine;  

candyMachine 객체 안의 속성을 찾아서 변수와 매칭해준다.
count 처럼 여러 단계 안의 속성도 찾을 수 있다.
getCandy와 count 변수가 초기화 된 것


배열도 비구조화 할 수 있다.

배열

var array = ['node.js',{],10,true];
var node = array[0];
var obj = array[1];
var bool = array[3];

위의 코드를 아래 코드로 바꿀 수 있다.

const array = ['node.js',{],10,true];
const [node,obj,,bool]=array;
profile
풀스택 개발자를 꿈 꾸는 릭터입니다.

0개의 댓글