Leonkennedy2 最近的时间轴更新
Leonkennedy2

Leonkennedy2

V2EX 第 626083 号会员,加入于 2023-04-26 19:06:08 +08:00
[业主被物业公司起诉支付物业费] 的应对之策
  •  1   
    问与答  •  Leonkennedy2  •  43 天前  •  最后回复来自 qq1147
    13
    出了一场小车祸,事后带着恨意卸载网易云音乐
  •  6   
    深圳  •  Leonkennedy2  •  96 天前  •  最后回复来自 heartlose
    273
    看到一个侵权“开源软件”的判决书
    分享发现  •  Leonkennedy2  •  144 天前  •  最后回复来自 adian
    54
    关于假手机纠纷在胜诉后的执行结果
    深圳  •  Leonkennedy2  •  156 天前  •  最后回复来自 Leonkennedy2
    24
    [GLINET] 路由器开发岗位招聘
    酷工作  •  Leonkennedy2  •  284 天前  •  最后回复来自 Leonkennedy2
    1
    Centos7.9 如何优雅地批量添加 IPv6 地址
    宽带症候群  •  Leonkennedy2  •  274 天前  •  最后回复来自 Leonkennedy2
    10
    Leonkennedy2 最近回复了
    我一直用虚拟机...
    写个间谍脚本,上报 bash 的 history ,如果有必要可以根据请求生成一个 root 账户登陆进去
    9 天前
    回复了 imherer 创建的主题 程序员 ts 里类似 golang 这种写法怎么实现?
    我问了一下克劳德

    在 TypeScript 中,您可以通过几种方式来实现类似 Golang 的功能。以下是一些可能的方法:

    1. 使用枚举和命名空间结合:

    ```typescript
    enum StatusEnum {
    Success = 1,
    Failed = 2
    }

    namespace Status {
    export const Success = StatusEnum.Success;
    export const Failed = StatusEnum.Failed;

    export function toString(status: StatusEnum): string {
    switch (status) {
    case StatusEnum.Success:
    return "成功";
    case StatusEnum.Failed:
    return "失败";
    default:
    return "未知";
    }
    }
    }

    // 使用
    console.log(Status.toString(Status.Success)); // 输出:成功
    ```

    2. 使用类和静态方法:

    ```typescript
    class Status {
    static readonly Success = new Status(1, "成功");
    static readonly Failed = new Status(2, "失败");

    private constructor(public readonly id: number, public readonly value: string) {}

    toString(): string {
    return this.value;
    }
    }

    // 使用
    console.log(Status.Success.toString()); // 输出:成功
    console.log(Status.Failed.value); // 输出:失败
    ```

    3. 使用对象和函数:

    ```typescript
    const Status = {
    Success: 1,
    Failed: 2,
    toString(status: number): string {
    switch (status) {
    case Status.Success:
    return "成功";
    case Status.Failed:
    return "失败";
    default:
    return "未知";
    }
    }
    } as const;

    // 使用
    console.log(Status.toString(Status.Success)); // 输出:成功
    ```

    4. 使用 Symbol 和对象:

    ```typescript
    const Status = {
    Success: Symbol('Success'),
    Failed: Symbol('Failed'),
    toString(status: symbol): string {
    switch (status) {
    case Status.Success:
    return "成功";
    case Status.Failed:
    return "失败";
    default:
    return "未知";
    }
    }
    };

    // 使用
    console.log(Status.toString(Status.Success)); // 输出:成功
    ```

    这些方法中,第 2 种(使用类和静态方法)可能最接近您的 Golang 示例。它允许您使用`Status.Success.toString()`来获取字符串描述,同时保持了类型安全性。

    选择哪种方法取决于您的具体需求和偏好。每种方法都有其优点和适用场景。
    9 天前
    回复了 flyPig9527 创建的主题 分享创造 新鲜出炉的相机水印网站工具
    @flyPig9527 就比如说我还有个奥林巴斯相机,这是个小众相机。提供自定义 logo 还是挺有用的,或者干脆让输入文字,弄些艺术字体
    9 天前
    回复了 CHS 创建的主题 分享发现 曝光无良公司被威胁删帖
    几年前买过紫田的服务器,中规中矩,后来有更合适的托管就搬走了
    9 天前
    回复了 flyPig9527 创建的主题 分享创造 新鲜出炉的相机水印网站工具
    @flyPig9527 牛逼
    闲鱼上有人整备的 N3160 x86 主机,二百块钱左右
    11 天前
    回复了 flyPig9527 创建的主题 分享创造 新鲜出炉的相机水印网站工具
    @flyPig9527 对对, 也是这个意思,你这个站只有有限几个设备的 logo ,毕竟拍摄设备那么多
    11 天前
    回复了 flyPig9527 创建的主题 分享创造 新鲜出炉的相机水印网站工具
    @flyPig9527 品牌 logo ,比如我照片事华为拍的,我想自定义小米拍的
    11 天前
    回复了 flyPig9527 创建的主题 分享创造 新鲜出炉的相机水印网站工具
    等个自定义 logo ,我想弄个手机 logo 的图
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2550 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 02:06 · PVG 10:06 · LAX 19:06 · JFK 22:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.