繁体   English   中英

访问嵌套方法中的私有属性 class 和 function?

[英]access class private property inside nested method and function?

我正在尝试访问嵌套 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通常是指当前的 function。如何在构造函数中的函数内访问 2 实例中的状态方法?

this不指向Logger实例,在本例中它指向您返回的log属性 object。您可以使用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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM