typescript 中的 export 和 public 有什么区别? [英] What is the difference between export and public in typescript?
问题描述
我认为这是对 这个问题.也许这个问题的措辞更好,你什么时候会使用公共,而不是出口?从我的阅读来看,似乎在 C#/Java 人员认为公开的任何地方,您真正想要的是导出.
I think this is a different slant on this question. And maybe the question is better phrased, when would you use public, as opposed to export? From my reading it seems like anywhere a C#/Java person thinks public, what you actually want is export.
您何时/何地使用 public 而不是 export?
When/where would you use public instead of export?
推荐答案
public
作为可见性修饰符在技术上什么都不做(所有类成员默认都是公开的);它作为 private
的显式对应物存在.只有在课堂上才合法.
public
as a visibility modifier technically does nothing (all class members are public by default); it exists as an explicit counterpart to private
. It's legal only inside classes.
export
根据上下文(在文件中的顶级成员或在 module
块中)执行两种不同的操作.
export
does two different things depending on its context (on a top-level member in a file or in a module
block).
在文件的顶层,export
意味着包含的文件是一个外部模块(即它将使用 RequireJS 加载,Node 的 require
命令,或其他一些符合 CommonJS/AMD 的加载器),并且您放置 export
的符号应该是该外部模块的导出成员.
At the top level of a file, export
means that the containing file is an external module (i.e. it will be loaded using RequireJS, Node's require
command, or some other CommonJS/AMD-compliant loader) and that the symbol you put export
on should be an exported member of that external module.
在 module
块内,export
意味着指定的成员在该模块块外可见.module
块中事物的默认设置是闭包隐私"——未导出的对象在模块外是不可见的.当 module
内的声明具有 export
修饰符时,它反而成为可以从模块外部访问的模块对象的属性.
Inside a module
block, export
means that the specified member is visible outside that module block. The default for things in module
blocks is "closure privacy" -- unexported objects are not visible outside the module. When a declaration inside a module
has the export
modifier, it instead becomes a property of the module object that can be accessed from outside the module.
语言中没有地方 public
和 export
都是合法的,因此在这方面选择相对容易.
There is no place in the language where both public
and export
are legal, so choosing is relatively easy in that regard.
这篇关于typescript 中的 export 和 public 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!