Private “functions” in TypeScript

前端 未结 3 361
梦谈多话
梦谈多话 2020-12-02 14:55

Is it possible to create a private \"function\" (method) within a TypeScript class? Let\'s assume we have the following Person.ts TypeScript file:



        
3条回答
  •  清歌不尽
    2020-12-02 15:49

    You can use private class variables and functions in TypeScript/Webpack and even in the latest Google Chrome versions natively. Just add a # before the name. See here.

    MyClass{
    
      #privateMember: Number = 4 
    
      #privateMethod() {
          this.#privateMember = 3
      }
    }
    

提交回复
热议问题