zoukankan      html  css  js  c++  java
  • TypeScript Crash Course: Property Access Modifiers

    There is no other great moment to head into the world of TypeScript instead of right now. Angular is in TypeScript, React is in TypeScript, and even Vue3 is in TypeScript. That means it's a skill we must equip with rather than wait and see.

    This is the first post of my own TypeScript crash course, chances that it's your jam, stay tune;)

    public, private, protected and readonly access modifier

    • public the default access modifier for properties

    • private lock the properties inside the cage, no one else except the class in where it's defined can access it. But we could approach it in JavaScript runtime, even by valueOf method.

    • protected as the same as private, but open a backdoor for derived class.

    • readonly the properties marked with readonly should be initialized either when declare in no time or inside constructor. But it only works in TypeScript.

      /* compile time */
      class User {
           readonly idCard: string
           constructor(idCard: string) {
               this.idCard = idCard
           }
      }
      
      let user = new User('123')
      user.idCard = '321' // error hint
      
      /* runtime */
      user.idCard = '321'
      console.log(user.idCard) // "321", the value of idCard has been changed.
      
      // solution for real readonly properties
      
      class User {
           readonly idCard: string
           constructor(idCard: string) {
               this.idCard = idCard
               Object.defineProperty(this, 'idCard', { writable: false })
           }
      }
      /* runtime */
      user.idCard = '321' // everything goes well
      console.log(user.idCard) // but it's still "123"
      

    Define properties through constructor parameters

    It's way too boring to put values into the properties when construct an instance like below

    class User {
        private readonly idCard: string
        protected name: string
        age: number
    
        constructor(idCard: string, name: string, age: number) {
            this.idCard = idCard
            this.name = name
            this.age = age
        }
    }
    

    Fortunately, TypeScript has done that for us. When we specify public, private or other access modifiers on the constructor parameters, a corresponding property is created on the class and filled with the value of the parameter. So we could make the previous one much damn shorter like this.

    class User {
        constructor(private readonly idCard: string, protected name: string, public age: number) {}
    }
    

    Pretty cool yet;)

    欢迎添加我的公众号一起深入探讨技术手艺人的那些事!

    如果您觉得本文的内容有趣就扫一下吧!捐赠互勉!
      

  • 相关阅读:
    bash shell学习shell基础 (笔记)
    Linux入门 (笔记)
    《Effective C++》 阅读小结 (笔记)
    趣味PAT循环19. 币值转换(20)
    git学习小结 (笔记)
    一个新手Java编程的初次感受
    201671010112 第四周的感悟
    201671010112 老同学的java学习之路
    全民IT时代到来了?学计算机很有前途?——淘宝2011校园招聘笔试感想
    编写友好的命令行应用程序
  • 原文地址:https://www.cnblogs.com/fsjohnhuang/p/15613248.html
Copyright © 2011-2022 走看看