在嵌套方法和函数中访问 class private 属性?

access class private property inside nested method and function?

我正在尝试访问嵌套函数中名为 status 的方法。

class Logger {
  #status = false;
  constructor(){
    console=(function(oldCons){
      return {
        log:function(text){
          if(this.status()){
            var e = new Error();
            if(!e.stack){
              try { throw e; } catch (e) { if(!e.stack){} }
            }
            var stack = e.stack.toString().split(/\r\n|\n/);
            for(var [key, step] of Object.entries(stack)){ stack[key] = step.trim(); }
            if(text === ''){ text = '""'; }
            var timeElapsed = Date.now();
            var now = new Date(timeElapsed);
            var day = String(now.getDate()).padStart(2, '0');
            var month = String(now.getMonth() + 1).padStart(2, '0');
            var year = now.getFullYear();
            var hours = String(now.getHours()).padStart(2, '0');
            var minutes = String(now.getMinutes()).padStart(2, '0');
            var secondes = String(now.getSeconds()).padStart(2, '0');
            var date = year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+secondes;
            oldCons.log('['+date+']',text);oldCons.log(stack);
          }
        },
        info:function(text){ oldCons.info(text); },
        warn:function(text){ oldCons.warn(text); },
        error:function(text){ if(this.status()){ oldCons.error(text); } }
      };
    }(window.console));
    window.console = console;
    }
  status(){ return this.#status; }
  enable(){ this.#status = true; }
  disable(){ this.#status = false; }
  toggle(status = null){
    if(status == null){
      if(this.#status){ this.disable(); } else { this.enable(); }
    } else { this.#status = status; }
  }
}

const Log = new Logger();

但我最终遇到了一个错误:Uncaught TypeError: this.status is not a function。我理解 this 通常指的是当前函数。如何在构造函数中的函数内访问 2 实例中的状态方法?

this 不指向 Logger 实例,在这种情况下它指向 return 对象的 log 属性。 您可以使用对 this 的引用,如以下示例 self:

class Logger {
  #status = false;
  constructor() {
    const self = this;

    console = (function(oldCons) {
      return {
        log: function(text) {
          if (self.status()) {
            var e = new Error();
            if (!e.stack) {
              try {
                throw e;
              } catch (e) {
                if (!e.stack) {}
              }
            }
            var stack = e.stack.toString().split(/\r\n|\n/);
            for (var [key, step] of Object.entries(stack)) {
              stack[key] = step.trim();
            }
            if (text === '') {
              text = '""';
            }
            var timeElapsed = Date.now();
            var now = new Date(timeElapsed);
            var day = String(now.getDate()).padStart(2, '0');
            var month = String(now.getMonth() + 1).padStart(2, '0');
            var year = now.getFullYear();
            var hours = String(now.getHours()).padStart(2, '0');
            var minutes = String(now.getMinutes()).padStart(2, '0');
            var secondes = String(now.getSeconds()).padStart(2, '0');
            var date = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + secondes;
            oldCons.log('[' + date + ']', text);
            oldCons.log(stack);
          }
        },
        info: function(text) {
          oldCons.info(text);
        },
        warn: function(text) {
          oldCons.warn(text);
        },
        error: function(text) {
          if (this.status()) {
            oldCons.error(text);
          }
        }
      };
    }(window.console));
    window.console = console;
  }
  status() {
    return this.#status;
  }
  enable() {
    this.#status = true;
  }
  disable() {
    this.#status = false;
  }
  toggle(status = null) {
    if (status == null) {
      if (this.#status) {
        this.disable();
      } else {
        this.enable();
      }
    } else {
      this.#status = status;
    }
  }
}

const Log = new Logger();