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
posted @ 2019-10-10 17:57  Heson  阅读(749)  评论(0编辑  收藏  举报
Live2D