1

아래 코드는 작동합니다. 가능하다면 한 면만 더 편리하게 사용할 수있는 방법이 있습니까?ES6 'this'로 구조 조정 지정

const { nextUrl, posts } = await postService.getCommunityPosts(6); 
this.communityPosts = posts; 
this.nextUrl = nextUrl; 

파괴 된 속성 별칭을 제공하는 것에 대해 알고 있지만이 경우 도움이된다고 생각하지 않습니다. MDN은이 사건에 대해 아무 말도하지 않습니다.

답변

5

별칭을 지정하고 괄호 안에 할당을 캡슐화하여 기존 객체의 속성에 할당 할 수 있습니다 (await codepen).

const demo = { nextUrl: 'nextUrl', posts: 'posts' }; 
 

 
const target = {}; // replace target with this 
 

 
({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo); 
 

 
console.log(target);

0
function Person() { 
    this.obj = { 
    firstName: 'Dav', 
    lastName: 'P' 
    }; 

    ({firstName: this.firstName, lastName: this.lastName} = this.obj); 
} 

let p = new Person(); 

console.log(p); 
+0

이 코드는 질문에 대답 수 있지만 , 방법 및/또는 그 대답의 장기적인 가치를 향상시킬 것입니다 문제를 해결하는 이유에 대한 추가 컨텍스트를 제공한다. –