匿名函数 ,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))
CSS margin border padding



文字相关:


本文探讨了JavaScript中的匿名函数和Promise的使用。通过add函数展示了回调函数的异步操作,然后通过hahah函数展示了如何利用Promise进行链式调用来处理异步操作。Promise的使用使得代码更加清晰,易于理解。同时,还演示了数组的方法如slice、splice以及函数参数的可选性和默认值。
645

被折叠的 条评论
为什么被折叠?



