为什么 Typescript 使用关键字“export”来公开类和接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15760462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Typescript use the keyword "export" to make classes and interfaces public?
提问by Grofit
While dabbling with Typescript I realised my classes within modules (used as namespaces) were not available to other classes unless I wrote the export
keyword before them, such as:
在涉足 Typescript 时,我意识到模块中的类(用作命名空间)对其他类不可用,除非我export
在它们之前写了关键字,例如:
module some.namespace.here
{
export class SomeClass{..}
}
So now I can use the above code like this:
所以现在我可以像这样使用上面的代码:
var someVar = new some.namespace.here.SomeClass();
However I was just wondering why this keyword is used opposed to just using the public
keyword which is used at method level to signify that a method or property should be externally accessible. So why not just use this same mechanism to make classes and interfaces etc externally visible?
但是,我只是想知道为什么使用此关键字而不是仅使用public
在方法级别使用的关键字来表示方法或属性应可从外部访问。那么为什么不使用相同的机制来使类和接口等在外部可见呢?
This would give resulting code like:
这将给出如下结果代码:
module some.namespace.here
{
public class SomeClass{..}
}
回答by Fenton
The primary reason is that export
matches the plans for ECMAScript. You could argue that "they should have used "export" instead of "public", but asides from "export/private/protected" being a poorly matched set of access modifiers, I believe there is a subtle difference between the two that explains this.
主要原因是export
匹配 ECMAScript 的计划。您可能会争辩说“他们应该使用“export”而不是“public”,但除了“export/private/protected”是一组匹配不佳的访问修饰符之外,我相信两者之间存在细微的差异来解释这一点.
In TypeScript, marking a class member as public
or private
has no effect on the generated JavaScript. It is simply a design / compile time tool that you can use to stop your TypeScript code accessing things it shouldn't.
在 TypeScript 中,将类成员标记为public
或private
对生成的 JavaScript 没有影响。它只是一个设计/编译时工具,您可以使用它来阻止您的 TypeScript 代码访问它不应该访问的内容。
With the export
keyword, the JavaScript adds a line to add the exported item to the module. In your example: here.SomeClass = SomeClass;
.
使用export
关键字,JavaScript 添加一行以将导出的项目添加到模块中。在您的例子:here.SomeClass = SomeClass;
。
So conceptually, visibility as controlled by public
and private
is just for tooling, whereas the export
keyword changes the output.
所以从概念上讲,由public
and控制的可见性private
仅用于工具,而export
关键字更改输出。
回答by Ryan Cavanaugh
A few things to add to Steve Fenton's answer:
Steve Fenton 的回答要补充几点:
export
alreadymeans two different things (depending on whether it's at top-level or not); making it mean a third is probably worse than addingpublic
/private
- It's definitely not to make the implementation easier; the added complexity of
public
vsexport
is trivial. We've changed keywords around a bunch already; it's not difficult. - The default visibility of class members mustbe public to align with the ES6 class proposal, therefore we need some keyword to indicate "not public". There isn't a suitable antonym to
export
(unexport
??), soprivate
is the logical choice. Once you haveprivate
, it would be somewhat insane to not choosepublic
as its counterpart - Use of
export
to modify visibility in internal modules is the best-guess alignment with ES6 modules
export
已经意味着两个不同的东西(取决于它是否在顶级);这意味着三分之一可能比添加public
/private
- 绝对不是为了让实现更容易;
public
vs增加的复杂性export
是微不足道的。我们已经改变了一堆关键字;这并不难。 - 类成员的默认可见性必须是 public 以与 ES6 类提案保持一致,因此我们需要一些关键字来表示“不公开”。
export
(unexport
??)没有合适的反义词,所以private
是合乎逻辑的选择。一旦你有了private
,不选择public
作为它的对手会有点疯狂 - 使用 of
export
来修改内部模块中的可见性是与 ES6 模块的最佳匹配