当前位置: 首页 > news >正文

04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化

1、类型别名type

// 类型别名type
// type 别名=类型()
type IType=string|number//别名的定义
interface demo{name:string,age:IType//别名的使用url:ResourceStr//type ResourceStr = string | Resource
}

2、检测数据类型

typeof 检测简单类型为主,instanceof 检测复杂类型为主

typeof检测数据类型

(以字符串的形式进行返回)

// typeof检测数据类型(以字符串的形式进行返回)
// 常规的返回值
console.log(`typeof01`,typeof 'ss')//string
console.log(`typeof`,typeof 12)//number
console.log(`typeof`,typeof true)//boolean
console.log(`typeof`,typeof undefined)//undefined
function fun(){}
console.log(`typeof`,typeof fun)//function
console.log(`------------------------------`,)
// 特殊场景的返回值
interface demo{name:string}
const obj:demo={ name:'' }
console.log(`typeof`,typeof NaN)//number   //NaN not a number
console.log(`typeof`,typeof null)//object
console.log(`typeof`,typeof [])//object
console.log(`typeof`,typeof obj)//object
console.log(`typeof`,typeof typeof obj)//string

instanceof-检测数据类型

//继承
class   Father{name:stringage:numberconstructor(name:string,age:number) {this.name=namethis.age=age}fatherFun(){console.log(`父类的方法`)}
}
// 定义子类继承父类的属性和方法
class Son extends Father{hobby:stringconstructor(name:string,age:number,hobby:string) {super(name,age)///调用父类的构造函数 并传递实参(写在子类属性的前面)this.hobby=hobby}sonFun(){console.log(`子类的方法`)}
}
const my:Son=  new Son('朱',18,'学习鸿蒙')
const YY:Son=  new Son('杨洋',20,'唱歌')
const dd:Son=  new Son('小李',18,'上学')
// instanceof----检测数据类型
//例如:检测实例化对象是不是属于这个类  实力化对象名  instanceof  类名
console.log(``,my instanceof Father)
console.log(``,YY instanceof Son)

3、空安全

空安全:   写代码的过程当中,有可能接收到null,一旦接受null程序会出错,这时需要处理错误

1,联合类型+判断

2,非空断言运算符 ! 

3,空值合并运算符 ??

 含义:??  左侧如果不是null 直接返回左侧的值   如果左侧是null 直接返回右侧的值
类比逻辑中断 | |   一样的用法

4,可选链?

// 空安全——>处理空值,保证程序正常运行
//1.联合 + 判断
//2.非空断言运算符 !
//3.合并运算符 ?? ( 类似逻辑中断 || )
// 可选链操作符 ?let num: number | null = null
// num=num+1  //出错,(可能为null)
// ---------解决----------
// 1、联合 + 判断
if (num !== null) {// 运算num = num + 1
}
// 2.非空断言运算符 !(主观告诉它,你不可能为空)
num = num! + 1// 3.合并运算符 ?? ( 类似逻辑中断 || )
// 4.可选链操作符 ?
function sum(x: number, y?: number) {// let xy:number=y||0let xy: number = y ?? 0let res: number = x + xy
}

4、剩余和展开(运算符 ...)

展开运算符


let arr1:number[]=[1,2,3]
let arr2:number[]=[4,5,6]
// 将数组1和数组2合并到数组3
//  展开运算符... 变量名  作用:取出数组中的数据
let arr3:number[]=[...arr1,...arr2]
console.log(``,arr3)//1,2,3,4,5,6let arr4:number[]=[1,2,3]
let arr5:number[]=[4,5,6]//将数组5的数据存放到数组4中
arr4.push(...arr5)
console.log(``,arr4)//1,2,3,4,5,6

剩余参数

// 剩余参数.,是使用在函数中
// function 函数名(参数:类型,...参数名:类型口)
function fun(a:number,b:number,...other:number[]){let total= a+b//检测一下other是不是一个数组console.log(``,other instanceof Array)//truefor(let item of other){// for of 循环数组,item是数组的每一项total+=item}return total
}
console.log(`结果1`,fun(1,2))
console.log(`结果2`,fun(3,4,5,6))

5.值类型和引用类型

简单类型和复杂类型

// 基本数据类型
let numA: number = 10
let numB: number = numA
numB++
console.log('numA:', numA) // ?
console.log('numB:', numB) // ?// 引用数据类型
class Person {name: string = ''constructor(name: string) {this.name = name}
}const p1: Person = new Person('jack')
const p2: Person = p1
// 修改 P2 是否会报错
p2.name = 'rose'console.log('p1.name:', p1.name) // rose
console.log('p2.name:', p2.name) // rose
// 值类型和引用数据类型的区别:
// 本数据类型(值类型/简单数据类型) 和引用数据类型(复杂数据类型)的区别
//1.分类:基本数据类型——>number string boolean undefined null
//       引用数据类型——>object function array
//2.存储:基本数据类型存储栈内存中  引用数据类型存储地址存储到栈中 数据存储到堆
//3.赋值:基本数据类型赋值是值本身  引用数据类型赋值是赋的地址

6、模块化

http://www.dtcms.com/a/338160.html

相关文章:

  • Maven依赖管理工具详细介绍
  • PowerShell定时检查日期执行Python脚本
  • 决策树的学习
  • 【EI会议征稿】2025第四届健康大数据与智能医疗国际会议(ICHIH 2025)
  • 基于STM32的电动车智能报警系统设计与实现
  • <数据集>遥感飞机识别数据集<目标检测>
  • rsync scp无法使用,踩坑破解之道!
  • 代理模式深度解析:从静态代理到 Spring AOP 实现
  • WAIC点燃人形机器人热潮,诠视SeerSense® DS80:多感融合的空间感知中枢,重新定义机器三维认知
  • 8月更新!Windows 10 22H2 64位 五合一版【原版+优化版、版本号:19045.6159】
  • 红日靶场01<超水版>
  • IDEA的创建与使用(2017版本)
  • 如何用企业微信AI 破解金融服务难题?
  • [Code Analysis] docs | Web应用前端
  • 深入解析:如何设计灵活且可维护的自定义消息机制
  • Spring AI + MCP Client 配置与使用详解
  • 专业高效的汽车部件FMEA解决方案--全星FMEA软件系统在汽车部件行业的应用优势
  • 百胜软件亮相CCDS2025-中国美妆数智科技峰会,解码美妆品牌数智化转型新路径
  • 【C语言16天强化训练】从基础入门到进阶:Day 2
  • 氯化铈:绿色科技的推动力
  • Tomcat Context的核心机制
  • LLM - windows下的Dify离线部署:从镜像打包到无网环境部署(亲测)
  • 【Goland】:Map
  • Golang資源分享
  • 第一阶段C#基础-13:索引器,接口,泛型
  • 线性调频信号(LFM)在雷达中的时域及频域MATLAB编程
  • 基于SFM的三维重建MATLAB程序
  • 分析慢查询
  • PPIO Agent沙箱:兼容E2B接口,更高性价比
  • 【DL学习笔记】损失函数各个类别梳理