Quick learning TypeScript - concise and key manual - Volume 2

👉 About the author

As we all know, life is a long process, constantly overcoming difficulties and constantly reflecting on the process of progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share all my thoughts, experiences and stories in order to find resonance!!!
Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
If there is any need to welcome private self, exchange group, so that learning is no longer lonely.

👉 premise

Non Xiaobai Wen, the author has several years of programming experience in developing Android and Unity. Because he wants to develop small games and learn TypeScript that cocos creator needs to use, he needs to understand the language knowledge. Xiaokong will pick the key points and simply bring them back without even mentioning them.

It's suitable for making a manual. It's OK to turn it over.

👉 Practice process

😜 function

In addition to the traditional nonparametric functions, parametric functions and return value functions, there are also the concepts of optional parameters and default parameters

        //Optional parameter function
        this.methodThree("My name");
        //Default parameter function
        //Indefinite parameter quantity function
        this.methodFive(0, 1, 2, 3);
        //Lunch anonymous function
        let methodNoNameOne = function () {
            return 1 + 2;
        //Parameterized anonymous function
        let methodNoNameTwo = function (numOne: number, numTwo: number) {
            return numOne + numTwo;
        console.log(methodNoNameTwo(1, 2))
        //The second is an optional parameter. It's ok if you don't pass it. What's the key?
        methodThree(myName: string, yourName?: string): void {


        //You can directly use = to set the default parameter. If it is not passed, the default value will be used to execute the method logic
        methodFour(myName: string = "Default parameters") {


        //Functions with uncertain parameters are similar to... In java
        methodFive(...nums: number[]) {


😜 Number property and method

Like most programming languages, global variables, local variables, static variables are not bad. Note that Xiao Kong is talking about TypeScript, not JavaScript. After the explanation of the above variable declaration and understanding the reason, you will find that it's great to write code in the future.

😜 String properties and methods

😜 array

This content is very important and frequently used, so it must be mastered.

//Array type
myNumberOneNum: number[] = [1, 2, 3, 4];
//Specify array length
myNumberOneNumOne: number[] = new Array(4);
//Create as new
myNumberOneNumTwo = new Array(1,2,3);
myNumberOneStr: string[] = ["1", "2", "3", "4"];
//Array can be any type, similar to List
myNumberTwo: Array<number> = [1, 2, 3, 4];
myNumberThree: Array<string> = ["1", "2", "3", "4"];
//Read only array - ensure that it is never modified after creation
zhiDu: ReadonlyArray<number> = [1,2,3];

😜 Map object

It is used to save key value pairs. There is a small note during creation, which will be explained in the following code. The basic properties are as follows:

map.clear() – remove all key value pairs of map objects, similar to clear of List.

map.set() – set key value pairs.

map.get() – returns the value corresponding to the key. If it does not exist, it returns undefined.

map.has() – returns a boolean type, which is used to determine whether the map contains the value corresponding to the key.

map.delete() – deletes the elements in the map, returns true if the deletion succeeds, and returns false if the deletion fails.

map.size – returns the length of the map object key value pair.

map.keys() - returns an Iterator object containing the keys of each element in the map object.

map.values() – returns a new Iterator object containing the values of each element in the map object.

//map object
    let mapOne = new Map([["keyOne", "valueOne"], ["keyTwo", "valueTwo"]]);
    //It includes clear, set, get, has, size and other methods
    mapOne.set("keyThree", "valueOne");
//Note that the following sentence will check for errors in the compiler. The small space uses webstorm
    mapOne.set("keyFour", 2);
    //It should be noted that if it is an initial value, the key and value formats need to be consistent when set ting, otherwise an error diagram will be prompted
    //If not, it can be as mapTwo below
    let mapTwo = new Map();
    mapTwo.set("keyOne", 1);
    mapTwo.set("keyTwo", "1");

Since it is a key value pair, we must use the data in the pair, which is needed for iteration. Iteration can be divided into three ways:

  1. The traditional for key is used to obtain the value
  2. Direct object form
  3. The entries() method of Map is used to perform for... Of
    //Use for..of to traverse the key key, and then use the get(key) of map to get value
    for (const keys of mapTwo.keys()) {
    //It can also be obtained iteratively in the form of objects
    for (const [key, value] of mapTwo) {
        console.log(key, value);
    //And the entries iteration of map
    for (const mapTwoElement of mapTwo.entries()) {
        console.log(mapTwoElement[0], mapTwoElement[1])

👉 other

📢 Author: Xiaokong and Xiaokong in Xiaozhi
📢 Reprint instructions - be sure to indicate the source: https://zhima.blog.csdn.net/
📢 Welcome to praise 👍 Collection 🌟 Leaving a message. 📝


Tags: Javascript TypeScript Unity

Posted on Sat, 30 Oct 2021 05:05:54 -0400 by dinno2