简体   繁体   English

解构但还返回源对象

[英]Destructuring but also returning the source object

One can do 一个可以做

import Foo, { bar } from 'foo-with-bar'

However, is something similar possible using destructuring , like 但是, 使用destructuring可能会发生类似的事情 ,例如

let { * as Foo, bar } = getFooBar()

?

Let's assume getFooBar() and the foo-with-bar module return 假设getFooBar()foo-with-bar模块返回

{ foo: 1, bar: a => console.log(a) }

and after the import / let , this is expected to print 1 : 并在import / let ,预期会打印1

bar(Foo.foo)

No, you cannot use a namespace import together with named imports (you can however use it together with a default import). 不,您不能将名称空间导入与命名导入一起使用(但是,可以将其与默认导入一起使用)。 You would have to use two separate import declarations: 您将必须使用两个单独的导入声明:

import * as Foo from 'foo-with-bar';
import { bar } from 'foo-with-bar';

No, if you're doing destructuring you already have split the object up in properties. 不,如果要进行结构分解,则已经在属性中拆分了对象。 However you can just use two assignments: 但是,您可以只使用两个分配:

let Foo = getFooBar();
let { bar } = Foo;

or: 要么:

let Foo, { bar } = Foo = getFooBar(); // very questionable and unmaintainable
let Foo = getFooBar(), { var } = Foo; // better

There is also a object rest property proposal with an experimental transpiler plugin that lets you do 还有一个带有实验性Transpiler插件的对象剩余属性建议 ,可让您执行

let { bar, ...Foo } = getFooBar();

but Foo will be a new object here and lack the bar property. 但是Foo将成为这里的新对象,并且缺少bar属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM