函数是JavaScript应用程序的基础。它帮助你实现抽象层,模拟类,信息隐藏和模块。
在TypeScript里,虽然已经支持类,命名空间和模块,但函数仍然是主要的定义行为的地方。
TypeScript为JavaScript函数添加了额外的功能,让我们可以更容易地使用
匿名函数
const a = function(b:string,c:string):string{
return b+c
}
有名函数
function aa(bb:string,cc:string):string{
return bb;
}
箭头函数
输出单个括号可以省略
const aaaaa = (time:number):void=>console.log(`2022年9月8日10:00:48${time}`);
const aaa = (time:number):void=>{
console.log(`2022年9月8日10:00:48${time}`);
}
接口函数
type aaaa=(x:number,y:number) =>number
const a3:aaaa=(x:number,y:number) =>x+y
可选参数
在TypeScript函数里,
如果我们定义了参数,则我们必须传入这些参数,
除非将这些参数设置为可选,
可选参数使用问号标识 "?"
const f2 = function(a:number,b?:number):number{
return a;
}
默认参数
我们也可以设置参数的默认值,
这样在调用函数的时候,如果不传入该参数的值,
const f3 = function(a:number=100,b:number=200,x:number=300):number{
return a+b+x;
}
函数的剩余参数
可传入多个参数
const f4 = function(...args:any[]){
console.log(args);
}
f4(14,50,60,90,"天天",true)
构造函数的使用
构造函数会在new一个对象创建时调用
let a = new Function("b","c","return b*c")
const a1 = a(10,200);
重载函数
重载是方法名字相同,而参数不同,返回类型可以相同也可以不同。
每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表
function add1(a:number,b:number):number
function add1(a:string,b:string,):string
function add1(a:string,b:number):number
function add1(a:number,b:string):number
function add1(a:any,b:any):any{
return a+b
}
参数数量不同时
function star(s1: string): string
function star(s1: number, n1: number): void
function star(s1: any, n1?: any): any {
let res = s1 + n1;
console.log(s1);
console.log(n1);
}
类的基本使用
Typescript是面向对象的JavaScript。类描述了所创建的对象共同的属性和方法。
TypeScript支持面向对象的所有特性,比如类、接口等。
class arr{
// 类作用域
}
lass Person{
name:string
age:number
// 字段
constructor(n:string,a:number){
this.name = n
this.age = a
// 构造函数
}
// 函数(方法)
sayHello():void{
console.log(`他叫${this.name},他今年${this.age}岁`);
}
}
类的继承
Typescript一次只能继承一个类,不支持继承多个类,但TypeScript支持多重继承(A继承B,B继承C)。
语法格式如下:c1ass child_c1ass_name(子类) extends parent_c1ass_name(父类)
class Person{
name:string
age:number
// 字段
constructor(n:string,a:number){
this.name = n
this.age = a
// 构造函数
}
// 函数(方法)
sayHello():void{
console.log(`他叫${this.name},他今年${this.age}岁`);
}
}
// extends 继承父类 super(值) 调用父类的值
class Stu extends Person{
score:string
constructor(name:string,age:number,score:string){
super(name,age)
this.score = score
// this它指向的是Stu
}
sayHello():void{
// super.sayHello() 手动调用父类方法 而且他不会被覆盖
console.log(`他叫${this.name},他今年${this.age}岁,他的成绩是${this.score}`);
}
}
let a2 = new Stu("香香",15,"SSS");
a2.sayHello()
static和instanceof
static关键字用于定义类的数据成员(属性和方法)为静态的,静态成员可以直接通过类名调用
class At{
static a1: string
static sayHello():void{
console.log(`我希望的工资是${At.a1}`);
}
}
At.a1 = "18k"
At.sayHello()
instanceof运算符
instanceof运算符用于判断对象是否是指定的类型,如果是返回true,否则返回false,
class Person{}
let p1 = new Person()
let p2= p1 instanceof Person
console.log(p2); //true
// 继承了父类
class Person1 extends Person{}
let t1 = new Person()
let t2 = t1 instanceof Person
console.log(t2); //true
TS修饰符
public(默认):公有,可以在任何地方被访问
protected:受保'。可以被其血守以及其子类访问
private:私有,只能被其定义所在的类访问。
class Person{
public name:string
protected age:number
private aa:string
// 字段
constructor(n:string,a:number,aa1:string){
this.name = n
this.age = a
this.aa = aa1
// 构造函数
}
// 函数(方法)
sayHello():void{
console.log(`他叫${this.name},他今年${this.age}岁`);
}
}
readonly:可以使用readon1y关键宁将属性设置为只读的。只读属性必须在声明时或枫迢函数里被初始化
class Aq{
readonly str1:string = "HTML"
readonly str2:string
readonly str3:string
readonly str4:string
readonly str5:string
constructor(str2:string, str3:string,str4:string,str5:string){
this.str2 = str2
this.str3 = str3
this.str4 = str4
this.str5 = str5
}
}
let pc = new Aq("q","W","e","r")
console.log(pc);
getters/setters
通过getters/setters来截取对对象成员的访问
注意点:如果存在get,但没有set,则该属性自动是只读的
如果没有指定setter 参数的类型,它将从getter的返回类型中推断出来
访问器和设置器必须有栖同的成员可见性
class Geta{
// 表示默认的私有属性
private full:string = "王页"//(private私有的)
// get从类里取值
get full1():string{
console.log("我已被get调用");
return this.full
}
// set是给类赋值
set full1(new1:string){
console.log("我已被set调用");
this.full = new1;
}
}
let a1 = new Geta()
a1.full1 = "袁冰妍"
console.log(a1);
console.log(a1.full1);
抽象类 abstract关键字
抽象类做为其它派生类的基类使用。它们一般不会直接被实例化
抽象类是专门用于定义哪些不希望被外界直接创建的类的
抽象类和接口一样用于约束子类
抽象类和接口区别
抽象方法必须包含abstract关键字并且可以包含访问修饰符
接口中只能定义约束,不能定义具体实现。
而抽象类中既可以定义约束,又可以定义具体实现
abstract class Person{
abstract name:string
abstract show():string
show1(){
console.log(this.show);
}
}
// 多继承抽象
class Stu extends Person{
name:string = "孟子义"
show():string{
return "陈情令"
}
}
let s = new Stu()
let res = s.show()
console.log(res);