typescript - TypeScript 类中的 # 符号是什么意思?

标签 typescript typescript-typings

例如,如果我有这个类(class):

type Url: string;

class A {
   #url: Url
}

# 符号的作用/代表什么?谢谢!

最佳答案

这是一个ECMAScript private field ,TypeScript 3.8 中的新增功能。该字段可以从类内部的方法访问,但不能从类外部访问。

更多信息:

文档中的示例:

class Animal {
    #name: string;
    constructor(theName: string) { this.#name = theName; }
}

new Animal("Cat").#name; // Property '#name' is not accessible outside class 'Animal' because it has a private identifier.

关于typescript - TypeScript 类中的 # 符号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60961043/

相关文章:

typescript - 为什么我需要在 Typescript 中输入 cast?

Angular 和 PouchDBFind 插件

javascript - 为现有 JavaScript 进行 TypeScript 声明

typescript - 如何在 typescript 中的 forEach 下定义类型?

javascript - Vue 类组件属性未在实例上定义

angular - 尝试将 Stomp over SockJS 添加到 Angular 应用程序 : TS2307 Cannot find module

angular - 如何将 Angular Material 扩展面板箭头图标定位在左侧

jquery - 如何在 TypeScript 中扩展 JQuery 3 选择器

typescript - 基于 Zod 模式将接口(interface)通用化

typescript - 为什么 Typescript Union + 映射类型在使用和不使用泛型的情况下工作方式不同