为什么要在ES2015中指定导出类型(let,var,const ...)? [英] Why to you have to specify the type of the export (let, var, const...) in ES2015?

查看:136
本文介绍了为什么要在ES2015中指定导出类型(let,var,const ...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是阅读这里,ES2015允许您导出 var const let function class default

As I'm reading here, ES2015 allows you to export var, const, let, function, class and default.

export var myVar1 = ...;
export let myVar2 = ...;
export const MY_CONST = ...;

export function myFunc() {
    ...
}
export function* myGeneratorFunc() {
    ...
}
export class MyClass {
    ...
}

但是我不明白为什么在我的外行人看来,应该有命名export 默认出口

But I don't understand why. In my layman opinion, there should be named exports and default exports.

您正在导出的类型似乎并不重要。我的意思是,当您 export default 时,您是否指定了类型?不,你没有,它的作品。另外,导出 var 还是 let 有什么区别?对 export const 有什么区别?当您导入模块时,它是不可变的(AFAIK)。

The type of what you are exporting doesn't seem to matter. I mean, when you export default, do you specify the type? No you don't, and it works. Additionally, what difference can it make to export var or let? What difference can it make to export const? When you import a module it's immutable anyway (AFAIK).

那么,为什么要指定导出的类型? p>

So, why do you have to specify the type of the export?

推荐答案

您不必指定导出的类型 - 必须指定模块中本地绑定的类型。

You don't have to specify the type of the export - you have to specify the type of the local binding in your module.


应该命名为export和default export。

there should be named exports and default exports.

有:

export {localX as exportedX};
export {localX as default};

您给出的所有这些示例实际上是shorthands,它们声明一个局部变量并将其导出相同的名称:

All those examples you've given are actually shorthands, which both declare a local variable and export it under the same name:

var myVar1 = …;
let myVar2 = …;
const MY_CONST = …;
function myFunc() {
    …
}
function* myGeneratorFunc() {
    …
}
class MyClass {
    …
}

export {
    myVar,
    myVar2,
    MY_CONST,
    myFunc,
    myGeneratorFunc,
    myClass
};




export const有什么不同?当您导入模块时,它是不可变的。

What difference can it make to export const? When you import a module it's immutable anyway.

您不能将重新分配到您的模块内。导出不导出值 1 ,它将绑定到您的局部变量。进口实际上并不是不可变的,它们是不可写的。

That you cannot reassign it inside your module. The export doesn't export a value1, it exports a binding to your local variable. Imports actually aren't immutable, they are only non-writable.

// example.js
export var value; // this one would not work with `const`
export default function(x) {
    value = x;
}

// main.js
import write, {value} from 'example';
console.log(value); // undefined
write(42);
console.log(value); // 42

1:默认导出在这方面有点特别。 export default ... 声明确实允许您直接导出表达式的值(或匿名函数/函数* / class 声明),但在幕后,它实际上在您的模块中创建了一个名为 * default * 的局部变量。

1: Default exports are a bit special in that regard. The export default … declaration does indeed allow you to directly export the value of an expression (or an anonymous function/function*/class declaration), but behind the scenes it actually does create a local variable in your module with the name *default*.

这篇关于为什么要在ES2015中指定导出类型(let,var,const ...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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