第二章 TypeScript
2.4 内置类型
2.4.1字符串
var name: string = 'wfz'
2.4.2 数字
var age: number = 45
2.4.3 布尔类型
var married: boolean = true;
2.4.4 数组
var jobs: Array<string> = ['IBM', 'Microsoft', 'Google']
var jobs: string[] = ['a', 'b', 'c']
数字型的数组
var jobs: Array<number> = [1, 2, 3]
var jobs: number[] = [1, 2, 3]
2.4.5 枚举
2.4.6 任意类型
var something: any = 'dggg'
something = 1
something = [1, 2, 3]
2.4.7 无类型
function setName(name: string): void {
this.name = name
}
2.5 类(内置类)
class Vehicle {
}
2.5.1 属性
class Person {
first_name: string;
last_name: string;
age: number;
}
2.5.2 方法
class Person {
first_name: string;
last_name: string;
age: number;
greet() {
console.log('ddd', this.first_name)
}
}
var p: Person = new Person()
p.first_name = 'wfz'
p.greet()
2.5.3 构造函数
class Person {
first_name: string;
last_name: string;
age: number;
constructor(first_name: string, last_name: string, age: number) {
this.first_name = first_name
this.last_name = last_name
this.age = age
}
greet() {
console.log('ddd', this.first_name)
}
}
var p: Person = new Person('wang', 'fengzi', 23)
p.greet()
2.5.4 继承
class Report {
data: Array<string>;
constructor(data: Array<string>) {
this.data = data
};
run() {
this.data.forEach(item => {
console.log(item)
})
}
}
class TableReport extends Report {
headers: Array<string>;
constructor(header: string[], values: string[]) {
super(values)
this.headers = headers
}
run() {
super.run()
}
}
var headers: string[] = ['name']
var data: string[] = ['a', 'b', 'c']
var r: TableReport = new TableReport(headers, data)
r.run()