匿名函数 ,Promise的使用:
function add(a: number, b: number, callback: (res: number) => void): void {
setTimeout(
() => {
callback(a + b)
}, 2000
)
// callback(a + b)
}
add(2, 3, res => {
console.log('2 + 3',res)
})
function hahah(a: number, b: number): Promise<number> {
return new Promise((resolve, reject) => {
setTimeout(()=> {
resolve (a + b)
}, 2000)
})
}
hahah(2, 3).then(res => {
console.log('2 + 3', res)
return hahah(res, 4)
}).then(res => {
console.log('5 + 5', res)
return hahah(res, 6)
}).then(res => {
})
编译命令:
npm run tsc
数组的基本用法
let a = [0,1,2,3,4,5,6,7,8]
console.log(a.slice(2, 5)) // [0,1,5,6,7,8,]
const deleted = a.splice(3,2,1,1,1,1)
console.log(a,deleted) // [0,1,2,1,1,1,1,5,6,7,8],[3,4]
const index = a.indexOf(2)//从左往右查
const index = a.lastIndexOf(2)//从右往左查
函数声明:
function add(
a: number,
b: number,
c?: number,
d: number=0,
...e: number[]): number {
let sum = a + b + (c || 0) + d
for(let i = 0; i < e.length; i++) {
sum += e[i]
}
return sum
}
const numbers = [5,6,7,8,9]
console.log(add(1,2,3,4,...numbers))