Transfer to become a reference manual for Typescript Programmers (iii)

Source: Internet
Author: User

Modules, types, interfaces

Typescript modules are used for code organization, similar to the C # namespace. A module can contain multiple classes and interfaces. Classes and interfaces can be privatized or exported, and the export means public, allowing other modules to access them.

Typescript class and C # have the same class meaning. In fact, one of the highlights of typescript is that he hides JavaScript's prototype design, but instead uses a more popular type-based object-oriented approach. You can extend other classes, implement multiple interfaces, add constructors, and expose properties and methods. These are similar to the C # class.

Properties and methods can be decorated with public or private access modifiers. When an access modifier is used on a constructor's argument, they automatically add a property of the same name to the type. Please be very careful with this syntax, which is different from many languages

The constructor parameter x, y of the point is used with the public access modifier, so the members of the point's variable × and y are automatically generated, which is a unique syntax for typescript.

in the figure Export keywords make classes and interfaces visible outside the module. The implementation interface uses the implements keyword, and the inheriting class uses the extends keyword, which is expressed differently from C # directly with a colon.

when you extend a class, use the Super keyword to invoke the method of the base class. Use The This keyword to invoke the properties and methods of the current class. The method to override the base class is optional. The constructor must call the constructor of the base class, and the compiler will alert you.

The module name contains points, and a module can be defined in multiple files. The name of the module is not too long, and typing will be more tiring when accessing it. is a more tiring example.

Parameters for functions (or functions)

Typescript's function parameters are rich in features: Optional parameters, default parameters, indeterminate parameters.

There are some different designs than other languages. These are described below.

Although these parameter features are not necessary in design, these typescript are somewhat special, and you need to understand them in case you can understand them.

The design of the optional parameters is basically consistent with C #, symbols used? Represents an optional parameter, and an optional parameter must appear after the required parameter.

The default parameter as long as the definition of the value is attached to the line, and unlike C #, the default parameters of typescript do not need to be constant, the runtime can be explained, the text will be described later

is a special default parameter

An indeterminate parameter can specify any number, which can be zero, and this design is similar to C #, just to add three points.

Here is a typical usage scenario for indeterminate parameters

function overloading

JavaScript is not allowed with duplicate functions, and the overloads implemented by Typescript are very different from C #.

The overload of Typescript is to write out all of the function signatures, writing an implementation, and the last function signature can contain all of the above function signatures.

Such as:

and C # Three overloaded functions have three function body different, typescript overload is actually all Overloadedmethod (Input:any) This last signature implementation, the above two is just two compatible signatures. However, with optional parameters, it is possible to show a similar invocation method to C # 's function overloading. The way to write a function is unavoidable to write a lot of IF.

Transfer to become a reference manual for Typescript Programmers (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.