简体   繁体   English

如何导出在ES6中具有保留名称的变量?

[英]How to export a variable which has reserved name in ES6?

As we know, this a syntax of ES6 of exporting a variable. 众所周知,这是导出变量的ES6语法。

export const LANGUAGE = 'JavaScript'

and this is a way of declaration of the same code in ES5: 这是在ES5中声明相同代码的一种方式:

exports.LANGUAGE = 'JavaScript'

but in some other cases it doesn't work, like for reserved words and for names which includes spaces: 但在某些其他情况下,则无法使用,例如保留字和包含空格的名称:

exports.true = '#true' exports['some text'] = 'text'

so what is the right way of declaring exports in ES6 ? 那么在ES6中声明出口的正确方法是什么?

You can't use the export const varName = 'Value' syntax with a reserved word; 您不能将export const varName = 'Value'语法与保留字一起使用; please read the following statement on ECMAScript 6 modules: the final syntax : 请阅读有关ECMAScript 6模块的以下声明:最终语法

Note that you can't use reserved words (such as default and new) as variable names, but you can use them as names for exports (you can also use them as property names in ECMAScript 5). 请注意,您不能将保留字(例如默认和新字)用作变量名,但可以将其用作导出的名称(在ECMAScript 5中也可以将它们用作属性名)。 If you want to directly import such named exports, you have to rename them to proper variables names. 如果要直接导入此类命名的导出,则必须将它们重命名为适当的变量名称。

According to that, it looks like you should be able to do something along the lines of: 据此,您似乎应该能够按照以下方式进行操作:

const true_ = '#true';

export { true_ as true };

Please note that this will also cause problems on the importing side as well. 请注意,这也会在导入方面引起问题。 You will most likely need to re-alias on import. 您很可能需要重新导入别名。 Eg, 例如,

import { true as true_ } from '...';

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

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