• TypeScript初识之枚举类型


    一、什么是弱类型语言?强类型语言?

    强类型是指不允许隐式变量类型转换,弱类型则允许隐式类型转换。

    换句话说:
    强类型语言,当你定义一个变量是某个类型,如果不经过代码显式转换(强制转化)过,它就永远都是这个类型,如果把它当做其他类型来用,就会报错 —编译型

    弱类型语言,你想把这个变量当做什么类型来用,就当做什么类型来用,语言的解析器会自动(隐式)转换。—解释型

    let a = 1;
    let b = '1';
    console.log(a == b)
    
    let a : number = 2;
    let b : string = 'b';
    let c : number
    c = a + b
    

    二、什么是静态类型语言?动态类型语言?

    静态类型语言:在编译阶段确定所有变量的类型(不可以在运行时改变变量的类型),所以在写程序时要声明所有变量的数据类型。

    • 对类型极度严格
    • 立即发现错误
    • 运行时性能好
    • 结构规范

    动态类型语言:在执行阶段确定所有变量的类型(可以在运行时改变变量的类型),也就是在用动态类型的语言编程时,永远也不用给任何变量指定数据类型,该语言会在你第一次赋值给变量时,在内部将数据类型记录下来。

    • 对类型非常宽松
    • Bug可能隐藏数月甚至数年
    • 运行时性能差
    • 可读性差

    ![image-20200818104748909](/Users/zhoupanpan/Library/Application Support/typora-user-images/image-20200818104748909.png)

    三、类型注解

    作用:相当于强类型语言中的类型声明

    语法:(变量 / 函数):type

    var [变量名] : [类型] = 值;
    

    TypeScript 的数据类型介绍

    // 原始类型
    let bool : boolean = true
    let num : number = 123
    let str : string = 'abc'
    
    // 数组
    let arr1 : number[] = [1, 2, 3]
    let arr2 : Array<number> = [1, 2, 3]
    let arr3 : Array<number | string> = [1, 2, 3, '4', '4']
    
    // 元祖 不能任意添加
    let tuple : [number, string] = [0, '3']
    
    // 函数
    let add = (x: number, y: number) => x + y
    let compute: (x: number, y: number) => number
    compute = (a, b) => a + b;
    
    // 对象
    let obj : {x: number, y: number} = { x: 1, y: 2 }
    obj.x = 3
    
    // symbol
    let s1 : symbol = Symbol()
    let s2 = Symbol()
    console.log(s1 === s2)
    
    // undefined, null
    let un : undefined = undefined
    let nu : null = null
    
    // void
    let noReturn = () => {}
    
    // any
    let x
    x = 1
    x = []
    x = () => {} // 不建议使用
    
    // never
    let error = () => {
        throw new Error('error')
    }
    
    // 枚举内容无法修改
    // 数字枚举
    enum Role {
        Reporter = 1,
        Developer,
        Maintainer,
        Owner,
        Guest
    } 
    
    // 字符串枚举 
    enum Message {
        Success = '恭喜你,成功了',
        Fail = '抱歉,失败了'
    }
    
    // 异构枚举
    enum Answer {
        N,
        Y = 'yes'
    } // 易引起混淆 不建议使用
    
    
    // 枚举成员 在需要被计算的后面定义变量 必须要给初始值
    enum Char {
        // const 
        a,
        b = Char.a,
        c = 1 + 3,
        // computed
        d = Math.random(),
        e = '123'.length,
    }
    
    // 常量枚举 编译后会清空,多用于不需要对象,只需要对象的值时,减少编译环境的代码
    const enum Month {
        Jan = 3,
        Feb,
        Mar
    }
    let month = [Month.Jan, Month.Feb, Month.Mar]
    
    
    // 枚举类型
    enum E { a, b }
    enum F { a = 0, b = 1 }
    enum G { a = 'apple', b = 'banana' }
    
    let e : E = 3
    let f : F = 3
    // e === f
    
    let e1 : E.a = 1
    let e2 : E.b
    // e1 === e2
    let e3 : E.a = 1
    // e1 === e3
    
    let g1 : G = G.b   // 字符串只能是其自身
    let g2 : G.a = G.a
    // g1 === g2
    

    总结:若将javascript比作一匹野马,那么typescript则是控制野马的缰绳。有了它,我们将在前端领域更好的驰骋,避免出错,避免代码上线脊背发凉。。。

  • 相关阅读:
    火狐黑客插件
    使用POI对EXCEL 读入写出
    使用spring quartz实现定时任务
    toad for oracle 快捷键总结
    Oracle查询性能优化
    2.C语言中文网学习Python
    1.编程基础(C语言中文网)
    一键打开ASP.NET WEB网站项目
    解决VS2010无法添加Sql Server数据库的问题
    VS2010 的一个小Bug(已报告给Microsoft Connect并得到确认)
  • 原文地址:https://www.cnblogs.com/zpsakura/p/13524458.html
Copyright © 2020-2023  润新知