programing

개체를 이미 정의 된 변수로 분해하는 방법은 무엇입니까?

nasanasas 2020. 11. 20. 09:01
반응형

개체를 이미 정의 된 변수로 분해하는 방법은 무엇입니까?


이 질문에 이미 답변이 있습니다.

다음은 구문 오류를 생성합니다.

let source,
    screenings,
    size;

source = {
    screenings: 'a',
    size: 'b'
};

{
    screenings,
    size
} = source;

예상 결과:

screenings should be equal to 'a'
size should be equal to 'b'

선언 구문 없이 할당 을 사용해야 합니다.

({
    screenings,
    size
} = source);

Babel REPL 예제

링크 된 문서에서 :

선언없이 객체 리터럴 비 구조화 할당을 사용할 때 할당 문 주위의 (..) 구문이 필요합니다.

그리고 분명히 let변수를 재 선언 할 수 없기 때문에 이것을 사용해야 합니다. 을 사용하는 경우 var다시 선언 할 수 있습니다.var { screenings, size } = source;

참고 URL : https://stackoverflow.com/questions/32138513/how-to-destructure-an-object-to-an-already-defined-variable

반응형