How to define private constructors in javascript?

前端 未结 4 1210
旧巷少年郎
旧巷少年郎 2020-12-25 15:09

I have defined pure objects in JS which expose certain static methods which should be used to construct them instead of the constructor. How can I make a constructor for my

4条回答
  •  借酒劲吻你
    2020-12-25 15:21

    Another possible simple approach is to use predicate function instead of instanceof. For typescript it can be a type guard and type synonym instead of a class can be exported:

    // class is private
    class _Score {
      constructor() {}
    }
    
    export type Score = _Score
    
    export function isScore(s): s is Score {
      return s instanceof _Score
    }
    

提交回复
热议问题