Home >Web Front-end >JS Tutorial >C# programmers' understanding process of TypeScript_javascript skills

C# programmers' understanding process of TypeScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:54:041427browse

Introduction

TypeScript has been developing well. When our company develops new features, we consider the maintainability of the program and use TypeScript to write programs on the browser. We use TypeScript from scratch, and even I am only half-capable of javascript. This article describes the process of a c# programmer getting to know TypeScript.

Note: This article is written based on Typescript 0.8 version, and it is new to use and may be outdated. For specific specifications, please refer to http://www.typescriptlang.org

Namespaces and classes

As an object-oriented developer, the first thing that comes to mind is how TypeScript defines classes. Since the principles of our project server (C#) are exactly the same as those of the client (TypeScript), we just use C# and TypeScript here. contrast.

C# declaration class

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

TypeScript declaration class

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

First of all, to put it simply, there are concepts similar to namespaces. One is called namespace and the other is called module. This is not nonsense.

Secondly, to reference other classes in C#, first you need to reference the dll in the project file, and then use a namespace (optional) in the file header. However, in TypeScript, there is no such concept, and a file is directly referenced. .

In C#, classes can have many levels such as public, internal, etc., as well as modifiers such as sealed. Forget about these in TypeScript. Adding export is equivalent to public, abstract, value type, etc. This seems not to be available.

But there are interfaces.

Methods and comments

C# method

    /// <summary>
    ///  解码变更集
    /// </summary>
    /// <param name="reader"> 一个变更集读取器对象 </param>
    public void DecodeChangeSet(ChangeRecordReader reader) {
      //解码上下文对象
      var ctx = new DecodeContext();

TypeScript declaration method

    /**
     * 传入变更集,将其解码到当前的对象容器。
     * @param {System.Erp.VirtualUI.Engine.IChangeRecordReader} reader - 提供记录集。
     */

    public Decode(reader: IChangeRecordReader): void {
      //解码上下文对象
      var ctx = new DecodeContext();

We first see that c#’s xml document-specific comments are also supported, unlike other JsDoc specifications.

Ordinary comments also use //, which is exactly the same as javascript.

In the declaration of a method, TypeScript puts the return parameter at the end. Correspondingly, the type of the parameter is also placed after the name. If you declare a variable, the same is true

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

var item: VirtualObject; //Define 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 distinguish them, but this is not allowed in JavaScript, so TypeScript does not allow it.

Due to the above reasons, you can also understand that there can only be one constructor. The following is 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 such as ref out in, but there are named access parameters and optional parameters.

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

Okay, for more details you need to study the specification document slowly. This document can help you get started and enjoy using it.

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn