首页 > 其他分享 >兼收并蓄 TypeScript - 基础: 基础

兼收并蓄 TypeScript - 基础: 基础

时间:2024-09-20 12:04:08浏览次数:1  
标签:10 操作数 TypeScript console log 基础 兼收并蓄 let null

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 基础: 基础

示例如下:

basic\basic.ts

{
    // 基础

    // try/catch/finally 的用法
    function f1(str:string) : number | null {
        try {
            let num = Number(str);
            if (isNaN(num)) {
                throw new Error("不是一个有效的数字");
            }
            return num / 5;
        } catch (error) {
            console.error(`catch: ${(error as Error).message}`);
            return null;
        } finally {
            console.log("finally");
        }
    }
    
    console.log(f1("100")); // finally 20
    console.log(f1("abc")); // catch: 不是一个有效的数字 finally null
}

// es2020 新特性
// 支持 ?? 运算符
{
    let value1: string | null = null;
    let value2: string | undefined = undefined;
    let value3: string = 'Hello';
 
    // ?? 用于在变量为 null 或 undefined 时提供默认值
    const result1 = value1 ?? 'Default';
    const result2 = value2 ?? 'Default';
    const result3 = value3 ?? 'Default';

    console.log(result1, result2, result3); // Default Default Hello
}

// es2021 新特性
{
    // ||= 仅当左侧操作数为假值时,才将右侧操作数赋值给左侧操作数
    let a = 10;
    a ||= 5; // 10
    let b = 0;
    b ||= 5; // 5

    // &&= 仅当左侧操作数为真值时,才将右侧操作数赋值给左侧操作数
    let c = 10;
    c &&= 5; // 5
    let d = 0;
    d &&= 5; // 0

    // ??= 仅当左侧操作数为 null 或 undefined 时,才将右侧操作数赋值给左侧操作数
    let e = null;
    e ??= 5; // 5
    let f = undefined;
    f ??= 5; // 5
    let g = 10;
    g ??= 5; // 10

    console.log(a, b, c, d, e, f, g); // 10 5 5 0 5 5 10
}

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:10,操作数,TypeScript,console,log,基础,兼收并蓄,let,null
From: https://www.cnblogs.com/webabcd/p/18422218/typescript_basic_basic

相关文章

  • 兼收并蓄 TypeScript - 基础: var, let, const
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:var,let,const示例如下:basic\var_let_const.ts//var声明的变量是全局作用域,块外也可用{vara=10;}console.log(a);//let声明的变量是块作用域,仅块内可用{......
  • 兼收并蓄 TypeScript - 基础: 数据类型
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:数据类型示例如下:basic\dataType.ts{//基本数据类型boolean,number,string,symbolleta:boolean=true;letb:number=10;letc:string="abc";......
  • 兼收并蓄 TypeScript - 基础: null, undefined
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:null,undefined示例如下:basic\null_undefined.ts{console.log(undefined==null,undefined===null);//truefalseconsole.log(typeofnull,typeofundefined);......
  • 兼收并蓄 TypeScript - 基础: boolean
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:boolean示例如下:basic\boolean.ts{leta=true;console.log(a);//true//将指定类型的数据转换为boolean类型console.log(Boolean(100),Boolean(......
  • AI基础 L22 Uncertainty over Time I 时间的不确定性
    TimeandUncertainty1TimeandUncertaintyStatesandObservations•discrete-timemodels:weviewtheworldasaseriesofsnapshotsortimeslices•thetimeinterval∆betweenslices,weassumetobethesameforeveryinterval•Xt:denotesthese......
  • JavaScript基础语法
    JavaScript基本语法一、变量及数据类型1.变量1.1.变量的定义方法在JavaScript中,你可以使用var、let或const关键字来声明变量。其中,var声明的变量会提升(hoisting)到其作用域的顶部,而let和const(ES6及以后版本引入)声明的变量具有块级作用域,并且const声明的变量一旦赋值后不可重新......
  • FLUENT离心风机仿真手把手零基础入门进阶有声解说教程(#331)
    本文摘要(由AI生成):本文主要介绍了FLUENT中旋转机械仿真方法,包括旋转坐标系模型、多参考坐标系模型、混和平面模型、滑移网格模型和重叠网格模型。其中,多重参考系法与滑移网格法设置几乎相同,两者相互转换容易。本文以离心风机为例,介绍了使用WORKBENCH19.2平台及其对应的DM、ICEM......
  • 零基础如何学习挖漏洞?零基础入门到精通,收藏这一篇就够了
    点击蓝字关注我们本栏目收录了【中国电信研究院安全技术研究所】在知乎平台关于安全问题的部分优质回答。在开始学习、研究漏洞之前,务必熟读《中华人民共和国网络安全法》!!!研究漏洞首先要对漏洞类型有一个大致的了解,根据工作需要或个人兴趣先选择一个方向深入学习,而不......
  • 多少岁转行网络安全不算晚?零基础想跳槽(转行)网络安全,看这篇就够了
    奉劝所有零基础想入门(转行)网络安全的朋友,麻烦转行前,一定要对网络安全行业做一个大概了解,不要一点都不懂就盲目转行。网络安全是什么?网络安全就是保障网络环境里的信息、数据安全,今年西北工业大学遭受网络攻击,武汉地震监测中心遭受网络攻击,便是典型的网络安全事件,一个国家......
  • 学习CentOs我们必备入门基础知识
    目录前言一、文件系统结构二、命令行操作三、软件包管理四、系统监控与管理五、用户与权限管理六、网络配置与管理七、系统安全与维护八、系统启动与关机总结前言CentOS(CommunityEnterpriseOperatingSystem)是一个基于RedHatEnterpriseLinux(RHEL)的开源操作系统......