san*_*oco 10 javascript destructuring variable-assignment ecmascript-6
以下代码有效.有没有一种更方便的方式,如果可能的话甚至是一个单行程?
const { nextUrl, posts } = await postService.getCommunityPosts(6);
this.communityPosts = posts;
this.nextUrl = nextUrl;
Run Code Online (Sandbox Code Playgroud)
我知道给出了结构化属性别名,但我不认为在这种情况下有帮助.MDN对此案没有任何说明.
Ori*_*ori 15
您可以通过给出别名并将括号括在括号中来分配现有对象的属性(等待代码集).
const demo = { nextUrl: 'nextUrl', posts: 'posts' };
const target = {}; // replace target with this
({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo);
console.log(target);Run Code Online (Sandbox Code Playgroud)
function Person() {
this.obj = {
firstName: 'Dav',
lastName: 'P'
};
({firstName: this.firstName, lastName: this.lastName} = this.obj);
}
let p = new Person();
console.log(p);Run Code Online (Sandbox Code Playgroud)