TypeScript随手

 Typescript多构造函数

不支持多个构造函数,可以使用可选参数

1
2
3
4
5
6
7
8
9
10
constructor(data?: IRefTeamSysMsgInfo) {
        if (data !== undefined) {
            this.id = data.id
            this.sendTime = data.sendTime
            this.msgType = data.msgType
            this.uid = data.uid
            this.state = data.status
        }
 
    }

  

c#中Action在ts中实现,在ts中的变量都是undefine

c# 中

1
2
3
4
5
6
public Action OnUpdate;
 
void Update()
{
   OnUpdate?.invoke(); 
}

  

ts中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export class Test{
 
beInit:boolean = false
 
    Init()
    {
      this.kcpHelper.OnUpdate=this.Update.bind(this)
    }
 
 
    Update()
    {
       KcpLog.I(`KcpManager.Update0  ${this.beInit} `) 
    }
}

  

如果不加bind(this) 则beInit输出undefine

 

函数参数中的?

export function autodata(dataKeyName?: string, type: JsProxyDataType = JsProxyDataType.Auto) {。中dataKeyName?中的?是什么意思

在 TypeScript 中,? 表示参数是可选的,可以在函数调用时省略该参数

 

enum取全部选项value或key

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
enum ServerCommand {
  // Request
  UserLoginRequest = 1001,
  // Response
  UserLoginResponse = 2001,
  // Sync
  TeamMsgSync = 3001,
  // Sync Ack
  SyncAck = 4001,
  // HeartBeat
  HeartBeat = 5001,
  // HeartBeat 应答
  HeartBeatAck = 5002,
}
 
const enumKeys = Object.keys(ServerCommand) as (keyof typeof ServerCommand)[];
 
enumKeys.forEach((key) => {
  console.log(key);
});
 
 
const enumValues = Object.values(ServerCommand) as (keyof typeof ServerCommand)[];
 
enumKeys.forEach((value) => {
  console.log(value);
});

  

 

posted @   00000000O  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2018-05-29 csdn地址备份
点击右上角即可分享
微信分享提示