c Programmer's understanding of TypeScript

  • 2020-06-15 07:48:10
  • OfStack

Introduction to the

TypeScript1 has been doing well. When our company developed new features, we used TypeScript to write programs for the browser due to the maintainability of the program. We started using TypeScript from scratch.

Note: this article is written based on Typescript0. 8 version, and early use, may be out of date, specific specification can be reference http: / / www typescriptlang. org

Namespaces and classes

As an object-oriented developer, the first thing that comes to mind is how TypeScript defines classes, and since the principles of our project server (C#) are exactly the same as those of the client (TypeScript), it's a good place to contrast C# with TypeScript.

C # class declaration


using System;
namespace Digiwin.Mars.VirtualUI.Engine {
internal sealed class Decoder {}
}

TypeScript class declarations


///<reference path="../Collections/ICollection.ts" />
module System.Erp.VirtualUI.Engine {
export class Decoder {}
}

First of all, there are some namespace-like concepts, one called namespace and one called module, so I'm not going to make any nonsense of this.

Second, for c# to refer to other classes, first you need to refer to dll in the project file, and then using1 namespace (optional) above the file, but in TypeScript, there is no such concept.

C# has classes at public, internal and so on, and modifiers like sealed, TypeScript, you forget about that, add export equivalent to public, abstraction, value type, that doesn't seem to exist.

But there are interfaces.

Methods and comments

The method of C #


    /// <summary>
    ///   Decoded changeset 
    /// </summary>
    /// <param name="reader"> 1 Three changeset reader objects  </param>
    public void DecodeChangeSet(ChangeRecordReader reader) {
      // Decode context object 
      var ctx = new DecodeContext();

TypeScript declares methods


    /**
     *  Pass in the changeset and decode it to the current object container. 
     * @param {System.Erp.VirtualUI.Engine.IChangeRecordReader} reader -  Provide a recordset. 
     */

    public Decode(reader: IChangeRecordReader): void {
      // Decode context object 
      var ctx = new DecodeContext();

We first saw that c#'s xml document-specific annotations are also supported, rather than using the JsDoc specification.

Normal annotations also use //, which is exactly the same as javascript.

On method declarations, TypeScript puts the return argument after the name, and, for that matter, the type of the argument after the name, if you declare a variable

private _maxId: number; // Define fields on the class

var item: VirtualObject; // Define variables in a method.

In terms of method accessibility, public is supported so that it is open or not.

Parameters and construction

In C#, we often define multiple methods with the same name, using different parameter types, but this is not allowed in javascript, so neither is TypeScript.

For the above reasons, you can understand that there can only be one constructor. Here's an example of his constructor:


    constructor(

      objectContainer: VirtualObjectContainer,

      objectBinder:IObjectBinder

    ) {

      this._objectContainer = objectContainer;

      this._binder = objectBinder;

    }

Based on the concept of javascript, there is no keyword like ref out in, but there are named access parameters and optional parameters.

I also didn't find the override keyword, although it is said that it was added after 0.8.

Well, more details are needed as you slowly delve into the specification documentation, which will help you get started and enjoy your use.

This is the end of this article, I hope you enjoy it.


Related articles: