C # programmer's understanding of TypeScript
This article shows you in detail the process of learning TypeScript from the perspective of C # programmers. It mainly compares the similarities and differences between the two languages and hopes to help you.
Introduction
TypeScript has been developing well. When developing new functions and considering the maintainability of the program, we used TypeScript to write the program on the browser. We started to use TypeScript from scratch, even javascript is a semi-jar. This article describes how a c # programmer understands TypeScript.
Note: This article is based on Typescript0.8 version, and the initial use, may be outdated, specific specifications can refer to the http://www.typescriptlang.org
Namespaces and Classes
As an object-oriented developer, the first thing that comes to mind is how TypeScript defines classes. Because the principle of the Project Server (C #) is exactly the same as that of the client (TypeScript, so here we will use C # to compare it with TypeScript.
C # Declaration class
?
1 2 3 4 |
Using System; Namespace Digiwin. Mars. VirtualUI. Engine { Internal sealed class Decoder {} } |
TypeScript Declaration class
?
1 2 3 4 |
/// <Reference path = "../Collections/ICollection. ts"/> Module System. Erp. VirtualUI. Engine { Export class Decoder {} } |
First of all, there is a concept similar to namespaces. One is namespace and the other is module.
Second, c # To reference other classes, first you need to reference dll in the project file, and then using a namespace (optional) on the file header, but in TypeScript, without this concept, you can directly reference a file.
In C #, classes can be public, internal, and many other levels, as well as modifiers such as sealed. In TypeScript, you should forget this. Adding export is equivalent to public, abstract, and value type. This does not seem to exist.
But there are some interfaces.
Methods and comments
C # Method
?
1 2 3 4 5 6 7 |
/// <Summary> /// Decode the change set /// </Summary> /// <Param name = "reader"> A Change set reader object </param> Public void DecodeChangeSet (ChangeRecordReader reader ){ // Decode the context object Var ctx = new DecodeContext (); |
TypeScript declaration method
?
1 2 3 4 5 6 7 8 |
/** * Input the change set and decode it to the current object container. * @ Param {System. Erp. VirtualUI. Engine. IChangeRecordReader} reader-provides a record set. */ Public Decode (reader: IChangeRecordReader): void { // Decode the context object Var ctx = new DecodeContext (); |
First, we can see that c #'s xml document special annotation is also supported, and different uses of JsDoc specifications.
Common annotations also use //, which is exactly the same as javascript.
In the declaration of a method, TypeScript places the return parameter behind it, and corresponding parameter types are also placed behind the name. If you declare a variable, this is also true.
Private _ maxId: number; // define a field on the class
Var item: VirtualObject; // defines variables in the method.
In terms of method accessibility, public is supported, so that it can be made public or not.
Parameters and Construction
In C #, we often define multiple methods with the same name and use different parameter types to differentiate them, but they are not allowed in javascript, so TypeScript is not allowed.
For the above reason, you can understand that there is only one constructor. The following is an example of his constructor:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Constructor ( ObjectContainer: VirtualObjectContainer, ObjectBinder: IObjectBinder ){ This. _ objectContainer = objectContainer; This. _ binder = objectBinder; } |
Based on the concept of javascript, there is no keyword such as ref out in, but there is a naming method to access parameters and optional parameters.
I have not found the override keyword, although it is said that it was added after 0.8.
Now, you need to study the standard documentation for more details. This document can help you get started and enjoy yourself.
The above is all the content of this article. I hope you will like it.