typescript 声明和解构

1.var 和 let

var hh:string = 'heson' //存在作用域提升
let hh1:string ='heson'

//块级作用域
function f1(flag:boolean):number{
    let a = 90
    if(flag){
        // let b = a +1  //ok
        var b = a + 10
        return b       //ok
    }
    //
    return b  //访问不到b
}
f1(true)

//注意 
/* 
function funa(x) {
    let x = 100 //不ok
} 
*/
/* 
function funb(flag:boolean,x:number) {
    if(flag){
        let x = 100
    }
}
 */

2.const 常量 可取但不可更改

const CAT_NAME:string = '喵喵'
 const CAT = {
    name:'mimi',
    age:5
 }
 // 直接修改是错误的
//  CAT = {
//      name:'xixi',
//      age:3
//  }

//但是可以修改对象上的属性
 CAT.name = 'xixi'

3.解构

//数组
let cc:number[] = [1,2]
let [one,two] = cc
console.log(one,two);

//对象
[one,two]=[two,one]
console.log(one,two)

let [first,...reset] = [1,2,3,4,5,6,7]
console.log(first) //1
console.log(reset) //[2,3,4,5,6,7]

let arrA = [4,5,6,7]
let arrB = [...arrA]
console.log(arrB)

//ts里面不要直接使用name关键字
interface Person{
    personName:string,
    personAge:number
}
//@ts-ignore
let personA:Person = {personName:'heson',personAge:18}

console.log(personA);
let {personName,personAge} = personA
console.log(personName,personAge)

最后附上运行代码

1 2
2 1
1
[ 2, 3, 4, 5, 6, 7 ]
[ 4, 5, 6, 7 ]
{ personName: 'heson', personAge: 18 }
heson 18

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐