医院网站建设ppt,wordpress 获取标签所有文章,seo优化论坛,兰州网站这里写自定义目录标题 变量条件控制循环函数类和接口模块开发 变量
TypeScript在JavaScript的基础上加入了静态类型检查功能#xff0c;因此每一个变量都有固定的数据类型。
let msg: string hello worldlet 声明变量的关键字#xff0c; const 则代表常量
msg 变量名称
因此每一个变量都有固定的数据类型。
let msg: string hello worldlet 声明变量的关键字 const 则代表常量
msg 变量名称string 变量的数据类型支持的变量类型
string 字符串可以使用单引号或者双引号
let msg: string hello world
number 数值整数浮点数都可以
let age: number 21
boolean布尔类型
let finished : Boolean true
any不确定类型可以是任意类型
let a: any jeck
union联合类型可以是多个指定类型中的一种
let u: string|number|boolean rose
u 18
object对象
let p {name:jack, age:21}
console.log(p.name)
console.log(p[name])
Array 数组元素可以是任意其他类型
let names Arraystring [jack, rose]
let ages: number[] [21, 18]
console.log(names[0])条件控制
TypeScript与大多数开发语言类似支持基于if-else和switch的条件控制。
// 定义数字
let num number 21// 判断是否是偶数
if (num % 2 0)
{console.log(num 是偶数)
}
else
{console.log(num 是奇数)
}
// 判断是否是正数
if (num 0)
{console.log(num 是正数)
}
else if (num 0)
{console.log(num 是负数)
}
else
{console.log(num 为0)
}
注意TypeScript中空字符串数字0nullundefined都被认为是false其他值则为true。
let grade: string Aswitch (grade)
{case A:{console.log(优秀)break}case B:{console.log(合格)break}default:{console.log(非法输入)break}
}
循环
TypeScript支持for和while循环并且为一些内置类型如Array等提供了快捷迭代语法。
// for循环
for (let i 1; i 10; i)
{console.log(i)
}
// while
let i 1;
while (i 10)
{console.log(i)i;
}// 数组
let names: string[] [jack,rose]
// for in 迭代器遍历得到数组的脚标
for (const i in names)
{console.log(i : names[i])
}
// for of 迭代器直接得到元素
for (const name of names)
{console.log(name)
}
函数
TypeScript通常利用function关键字声明函数并且支持可选参数默认参数箭头函数(匿名函数) 等特殊语法。
// 无返回值函数返回值void可以省略
function sayHello(name: string): void
{console.log(name)
}sayHello(jacik)// 有返回值函数function sum(x:number, y:number): number
{return x y
}let result sum(21,18)
console.log(result)// 箭头函数(匿名函数) let sayHi (name: string){console.log(name)
}
sayHi(rose)// 可选参数在参数名后面加表示该参数是可选的
function sayHello(name?: string)
{name name ? name: 123console.log(name)
}
sayHello()
sayHello(jack)//默认参数,在参数后面赋值表示参数默认值
//如果调用者没有传参则使用默认值function sayHello(name : string 123)
{console.log(name)
}
sayHello()
sayHello(jack)类和接口
TypeScript 具备面向对象编程的基本语法例如interfaceclassenum等也具备封装继承多态面向对象基本特征。
// 定义枚举
enum Msg
{HI Hi,HELLO hello
}// 定义接口抽象方法接受枚举参数
interface A
{say(msg: Msg): void
}// 实现接口
class B implements A
{say(msg:Msg): void{console.log(msg)}
}// 初始化对象
let a:A new B()
// 调用方法传递枚举参数
a.say(Msg.HI)// 定义 矩形类
class Rectangle
{// 成员变量private width: numberprivate length: number// 构造函数constructor(width: number, length: number){this. width widththis. Length length }// 成员方法public area(): number{return this.width * this. Length}
}//定义正方形
class Square extends Rectangle
{constructor(side: number){// 调用父类构造函数super(side, side)}
}let s new Square(10)
console.log(s.area())
模块开发
应用复杂时我们可以把通用功能抽取到单独的ts文件中每个文件都是一个模块module。模块可以相互加载提高代码复用性。
// 定义矩形类并通过export导出 rextangle.ts
export class Rectangle
{// 成员变量private width: numberprivate length: number// 构造函数constructor(width: number, length: number){this. width widththis. Length length }
}
// 定义工具方法求矩形面积并通过export导出
export function area(rec: Rectangle): number
{return rec.width * rec.Length
}// 通过import 语法导入from后面写文件的地址import {Rectangle, area} from ../rectangle
// 创建Rectangle对象
let r new Rectangle(10,20)
// 调用area方法
console.log(area(r))