本文介绍了在javascript es6中使用... spread语法命名为exports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图从库中导入所有内容作为哈希,修改它,并重新导出修改后的哈希,而不知道库中的所有命名导出。例如:

I am attempting to import everything from a library as a hash, modify it, and re-export the modified hash, without knowing all of the named exports in a library. For example:

import * as reactBootstrap from 'react-bootstrap';

wrappedReactBootstrap = doFunnyThingsTo(reactBootstrap);

export {
  ...wrappedReactBootstrap
};

// or
export wrappedReactBootstrap;

我对是规范不允许以下内容。有人可以确认吗?

My understanding of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export is that the following is not allowed by the spec. Could someone confirm?

显然,循环是不可能的,因为导出和导入语句必须是最高级别。

Obviously, looping is out of the question, since export and import statements must be top level.

推荐答案

对象休息传播是而不是任何规范的一部分(可能会包含在ES2018中)。

Object rest spread is stage 3 proposal and not a part of any spec (will probably be included in ES2018).

更重要的是, export 具有模仿现有JS语法的语法,但不会将 {...} 解释为表达式。 ,因为ES2015模块应该进行静态分析。这是它们的一个好处,但它要求开发人员明确指定导出和导入。

More importantly, export has syntax that mimics existing JS syntax but doesn't interpret { ... } as an expression. export syntax was strictly defined because ES2015 modules are supposed to be statically analyzed. This is one of their benefits, but it requires the developer to specify exports and imports explicitly.

因为 {... wrappedReactBootstrap} 引入了动态导出(它在这里用于此目的),ES2015模块 export 不支持它,它不太可能。

Since { ...wrappedReactBootstrap } introduces dynamic export (it was used here exactly for this purpose), it isn't supported by ES2015 module export and it is very unlikely that it will be.

如果有必要为导出提供动态行为,可以将其导出并导入为命名或默认对象。

If it is necessary to provide dynamic behaviour for the export, it can be exported and imported as named or default object.

import * as reactBootstrap from 'react-bootstrap';

export default doFunnyThingsTo(reactBootstrap);

并使用像

import wrappedReactBootstrap from '...';

const { funny, thing } = wrappedReactBootstrap;

显然, wrappedReactBootstrap 对象将无法获取ES2015模块以这种方式带来的好处,例如树木摇晃。

Obviously, wrappedReactBootstrap object won't get the benefits of ES2015 modules this way, e.g. tree-shaking.

这篇关于在javascript es6中使用... spread语法命名为exports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:55