TypeScript:接口 vs 类 vs 模块 vs 程序 vs 函数 [英] TypeScript: Interface vs Class vs Modules vs Program vs Function
问题描述
我阅读了位于以下位置的 TypeScript 规范:http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf
I read the TypeScript specification located at: http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf
但是它让我对以下内容感到困惑:
However it got me confused with following:
- 界面
- 班级
- 模块
- 程序
- 函数.
- 声明与 var
有人可以简要帮助理解什么时候应该使用上述哪一个?接口和类是否与 C# 版本相同?
Could someone briefly help to understand which one of above should be used when? Is Interface and Class same as C# version?
推荐答案
正如您在问题中提到的,我已经使这些答案与 C# 匹配,但希望这些答案对从类似语言来到 TypeScript 的人也有用.
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.
界面
TypeScript 中的接口类似于您在 C# 中遇到的接口.这是一个契约——如果你的一个类实现了一个接口,它承诺具有接口记录的某些属性或方法.
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 中,接口可以从另一个接口继承以扩展它,也可以从一个类继承以捕获其实现.
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).
课程
这与 C# 中类的概念非常相似.您可以从其他类继承以扩展或专门化行为.
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# 命名空间.
The newer namespace keyword is used to place a group of code within a limited scope. This is similar to C# namespaces.
模块
就 TypeScript 而言,模块比命名空间更好.模块(以前称为外部模块)是一个自包含的文件,不向全局范围添加任何内容.您可以根据需要将模块加载到局部变量中.模块提供了一种组织代码和按需加载部分的好方法.使用模块时,最好避免使用命名空间.模块优于命名空间.
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 编写的内容.
A program is a collection of modules, classes. This is essentially the thing you have written using TypeScript.
功能/方法
类包含方法,您也可以编写不属于类的独立函数.
Classes contain methods, and you can also write stand-alone functions that do not belong to a class.
声明与 var
var
创建一个新变量.declare
用于告诉 TypeScript 该变量已在别处创建.如果您使用 declare
,则不会在生成的 JavaScript 中添加任何内容 - 它只是对编译器的提示.
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 externalModule
的外部脚本,您将使用 declare var externalModule
来提示 externalModule
的 TypeScript 编译器代码>已经设置.
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.
这篇关于TypeScript:接口 vs 类 vs 模块 vs 程序 vs 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!