ES6使用`this`的销毁任务 [英] ES6 Destructuring assignment with `this`

查看:124
本文介绍了ES6使用`this`的销毁任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有效.有没有一种更方便的方法,甚至可能是单线的?

The code below works. Is there a way that is more convenient, if possible even a one-liner?

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

我知道为结构化属性提供别名,但是在这种情况下我认为这没有帮助. MDN 对此没有说什么

I know about giving destructured properties aliases but I don't think that helps in this case. MDN doesn't say anything about that case.

推荐答案

您可以通过提供别名并将分配值封装在括号中来分配给现有对象的属性(

You can assign to the properties of an existing object by giving aliases and encapsulating the assignment in parentheses (await codepen).

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

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

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

console.log(target);

这篇关于ES6使用`this`的销毁任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆