TypeScript:接口 vs 类 vs 模块 vs 程序 vs 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12764247/
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
TypeScript: Interface vs Class vs Modules vs Program vs Function
提问by Nil Pun
I read the TypeScript specification located at: http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf
我阅读了位于以下位置的 TypeScript 规范:http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf
However it got me confused with following:
然而,它让我对以下内容感到困惑:
- Interface
- Class
- Modules
- Programs
- Functions.
- Declare vs. var
- 界面
- 班级
- 模块
- 程式
- 职能。
- 声明与 var
Could someone briefly help to understand which one of above should be used when? Is Interface and Class same as C# version?
有人可以简要地帮助理解什么时候应该使用上述哪一个?接口和类是否与 C# 版本相同?
回答by Fenton
I have made these answers match C# as you've mentioned that in your question, but hopefully the answers are useful to people coming to TypeScript from similar languages too.
正如您在问题中提到的那样,我已经使这些答案与 C# 匹配,但希望这些答案对从类似语言来到 TypeScript 的人也有用。
Interface
界面
An interface in TypeScript is similar to those you have come across in C#. It is a contract - if one of your classes implements an interface, it promises to have certain properties or methods that the interface documents.
TypeScript 中的接口类似于您在 C# 中遇到的接口。它是一种契约——如果你的一个类实现了一个接口,它承诺具有接口记录的某些属性或方法。
In TypeScript an interface can inherit from another interface in order to extend it and from a class to capture its implementation.
在 TypeScript 中,接口可以从另一个接口继承以扩展它,也可以从类继承以捕获其实现。
Whenever something seems impossible in TypeScript, you can usually solve it with an interface!
每当在 TypeScript 中有些事情似乎不可能时,您通常可以使用接口来解决它!
In TypeScript, interfaces have a broad range of uses. They describe a structure, so can be used anywhere you use a type (i.e. not just to implement them in a class, you can use them to type variables, parameters, return values and so on).
在 TypeScript 中,接口有广泛的用途。它们描述了一个结构,因此可以在任何使用类型的地方使用(即不仅可以在类中实现它们,还可以使用它们来键入变量、参数、返回值等)。
Class
班级
This is very similar to the concept of a class in C#. You can inherit from other classes to extend or specialise the behaviour.
这与 C# 中类的概念非常相似。您可以从其他类继承以扩展或专门化行为。
Namespace
命名空间
The newer namespace keyword is used to place a group of code within a limited scope. This is similar to C# namespaces.
较新的命名空间关键字用于在有限范围内放置一组代码。这类似于 C# 命名空间。
Module
模块
Modules are better than namespaces when it comes to TypeScript. A module (previously known as an external module) is a file that is self contained and adds nothing to your global scope. You can load modules into local variables as you need them. Modules provide a good way to organise your code and load parts on demand. When using modules, it is best to avoid using namespaces. Modules are better than namespaces.
就 TypeScript 而言,模块比命名空间更好。模块(以前称为外部模块)是一个自包含的文件,不向全局范围添加任何内容。您可以根据需要将模块加载到局部变量中。模块提供了一种组织代码和按需加载部分的好方法。使用模块时,最好避免使用命名空间。模块比命名空间更好。
Program
程序
A program is a collection of modules, classes. This is essentially the thing you have written using TypeScript.
程序是模块、类的集合。这基本上是您使用 TypeScript 编写的内容。
Function / Method
功能/方法
Classes contain methods, and you can also write stand-alone functions that do not belong to a class.
类包含方法,您还可以编写不属于类的独立函数。
Declare vs. var
声明与 var
var
creates a new variable. declare
is used to tell TypeScript that the variable has been created elsewhere. If you use declare
, nothing is added to the JavaScript that is generated - it is simply a hint to the compiler.
var
创建一个新变量。declare
用于告诉 TypeScript 该变量已在别处创建。如果使用declare
,则不会向生成的 JavaScript 添加任何内容 - 它只是对编译器的提示。
For example, if you use an external script that defines var externalModule
, you would use declare var externalModule
to hint to the TypeScript compiler that externalModule
has already been set up.
例如,如果您使用定义 的外部脚本var externalModule
,您将使用declare var externalModule
提示externalModule
已设置的 TypeScript 编译器。