How to declare local function inside ES6 class?

前端 未结 2 2035
后悔当初
后悔当初 2021-01-02 12:26

I have a function that can be used only inside a class and don\'t want it to be accessible outside the class.

class Auth {
  /*@ngInject*/
  constructor($htt         


        
2条回答
  •  猫巷女王i
    2021-01-02 12:32

    You could use a Symbol:

    const localFunc = Symbol();
    class Auth {
      /*@ngInject*/
      constructor($http, $cookies, $q, User) {
        this.$http = $http;
        this.$cookies = $cookies;
        this.$q = $q;
        this.User = User;
    
        this[localFunc] = function() {
          // I am private
        };
      }
    }
    

提交回复
热议问题