typescript终极奥义(The ultimate meaning of typescript)

typescript终极奥义

1、函数

1.1函数定义

function test(){
    //函数体
    console.log(1111);
}

//函数定义 = function + 函数名 + 形参 + 函数体

1.2函数调用

函数名();

1.3函数返回值

function test1():number{    //限定函数的返回值类型
    console.log(2222);
    return 1;
}

1.4函数参数

function test2(str:string){   //限定形参的类型为string
    console.log("输入的字符串是"+str);
}

test2("666");

1.4.1可选参数

function test3(str:string , str1?:string){   //str1是可选参数
    console.log("输入的字符串是"+str+str1);
}

test3("777");
test3("888","999");

1.4.2默认参数

function test4(str:string , str1:string = "NB"){   //str1是默认参数
    console.log("输入的字符串是"+str+str1);
}

test4("111");
test4("222","666");

1.4.3剩余参数

可变参数 又叫多参数或参数数组

function test5(...nums:number[]){   //可变参数
    for (const key in nums) {
        console.log("可变参数"+nums[key])
    }
}

test5(1,2,3,4);

1.5匿名函数

没函数名

//匿名函数
let msg = function(){
    return "这是一个匿名函数";
}

console.log( msg() );

2、number

typescript(double)与c++(int float double)

2.1number类型的取值范围

console.log("最大值",Number.MAX_VALUE);
console.log("最小值",Number.MIN_VALUE);

2.2获取数值所对应的字符串

let num = 55;
console.log(typeof(num));
let str = num.toString();
console.log(typeof(str));
console.log(str);

3、string

要点:c++””代表字符串”代表字符,typescript””代表字符串和字符

3.1split方法,字符串切割方法

let str = "ni hao xion a";
let str1 = str.split(" ",3);    //参数1:按照什么分割,参数2:分成多少段
console.log(str1);

3.2length属性

获取字符串的字符数

let str = "ni hao xion a";
console.log(str.length);
//输出为13

3.3连接字符串 +

let str = "ni hao xion a";
let str1 = "1111";
let str2 = str+str1;
console.log(str2);

3.4截取字符串

let str1 = "1111";
console.log(str1.substring(0,2));   //左闭右开区间
//输出为11

console.log(str1.substring(1));  //输入一个参数,第二个参数默认为数组结尾
//输出为111

4、数组

4.1多维数组

数组中只能是单一类型,除非是限定any类型

let numDuo:number[][] = [[1,2,3],[3,3,3]];
console.log(numDuo[0][0]);
let numArr:any[] = [1,2,3,"依然是数组,不是元组"];		//声明数组为any类型

5、元组

数组的数据类型都是相同的(any类型的数组可以不同)

元组是存储的数据类型和个数是限定的

let numArr2 = [1,"元组"];	//元组
let numArr:any[] = [1,2,3,"依然是数组,不是元组"];//数组

6、类与对象

class stu {
    //类的成员变量
    id:number;
    name:string;

    //构造方法
    constructor(name:string,id:number) {
        this.name = name;
        this.id = id;
        
    }

    //类的成员方法
    getName():string{
        return this.name;
    }

    setName(name:string):void{
        console.log("调用了父类的setName方法");
        this.name = name;
    }
}


let zhang = new stu("黄同学",1);
zhang.setName("李同学");
console.log(zhang.getName());
console.log(zhang.id);




6.1继承与多态

//类的继承与多态
class son extends stu {
    
    getMajor():string{
        return "我是好学生";
    }

    //重写
    setName(name: string): void {
        super.setName(name);
    }
}

let li = new son("1",2);
console.log(li.getName());
console.log(li.getMajor());

————————

typescript终极奥义

1. Functions

1.1 function definition

function test(){
    //函数体
    console.log(1111);
}

//函数定义 = function + 函数名 + 形参 + 函数体

1.2 function call

Function name ();

1.3 function return value

function test1():number{    //限定函数的返回值类型
    console.log(2222);
    return 1;
}

1.4 function parameters

function test2(str:string){   //限定形参的类型为string
    console.log("输入的字符串是"+str);
}

test2("666");

1.4.1 optional parameters

function test3(str:string , str1?:string){   //str1是可选参数
    console.log("输入的字符串是"+str+str1);
}

test3("777");
test3("888","999");

1.4.2 default parameters

function test4(str:string , str1:string = "NB"){   //str1是默认参数
    console.log("输入的字符串是"+str+str1);
}

test4("111");
test4("222","666");

1.4.3 remaining parameters

Variable parameters are also called multiple parameters or parameter arrays

function test5(...nums:number[]){   //可变参数
    for (const key in nums) {
        console.log("可变参数"+nums[key])
    }
}

test5(1,2,3,4);

1.5 anonymous functions

No function name

//匿名函数
let msg = function(){
    return "这是一个匿名函数";
}

console.log( msg() );

2、number

typescript(double)与c++(int float double)

2.1 value range of number type

console.log("最大值",Number.MAX_VALUE);
console.log("最小值",Number.MIN_VALUE);

2.2 get the string corresponding to the value

let num = 55;
console.log(typeof(num));
let str = num.toString();
console.log(typeof(str));
console.log(str);

3、string

Important: C + + “” represents string ”, ” represents character, typescript “” represents string and character

3.1 split method, string cutting method

let str = "ni hao xion a";
let str1 = str.split(" ",3);    //参数1:按照什么分割,参数2:分成多少段
console.log(str1);

3.2length attribute

Gets the number of characters in the string

let str = "ni hao xion a";
console.log(str.length);
//输出为13

3.3 connection string+

let str = "ni hao xion a";
let str1 = "1111";
let str2 = str+str1;
console.log(str2);

3.4 intercepting string

let str1 = "1111";
console.log(str1.substring(0,2));   //左闭右开区间
//输出为11

console.log(str1.substring(1));  //输入一个参数,第二个参数默认为数组结尾
//输出为111

4. Array

4.1 multidimensional array

Only a single type can be in an array, unless it is a qualified any type

let numDuo:number[][] = [[1,2,3],[3,3,3]];
console.log(numDuo[0][0]);
let numArr:any[] = [1,2,3,"依然是数组,不是元组"];		//声明数组为any类型

5. Tuple

The data types of arrays are the same (arrays of any type can be different)

Tuples are the stored data types and the < strong > number is limited < / strong >

let numArr2 = [1,"元组"];	//元组
let numArr:any[] = [1,2,3,"依然是数组,不是元组"];//数组

6. Classes and objects

class stu {
    //类的成员变量
    id:number;
    name:string;

    //构造方法
    constructor(name:string,id:number) {
        this.name = name;
        this.id = id;
        
    }

    //类的成员方法
    getName():string{
        return this.name;
    }

    setName(name:string):void{
        console.log("调用了父类的setName方法");
        this.name = name;
    }
}


let zhang = new stu("黄同学",1);
zhang.setName("李同学");
console.log(zhang.getName());
console.log(zhang.id);




6.1 inheritance and polymorphism

//类的继承与多态
class son extends stu {
    
    getMajor():string{
        return "我是好学生";
    }

    //重写
    setName(name: string): void {
        super.setName(name);
    }
}

let li = new son("1",2);
console.log(li.getName());
console.log(li.getMajor());