Home > other >  access class private property inside nested method and function?
access class private property inside nested method and function?

Time:05-03

I am trying to access a method called status inside a nested function.

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();

But I end up with an error : Uncaught TypeError: this.status is not a function. I understand that this usually refer to the current function. How can I access the status method in the 2 instance inside the functions in the constructor?

CodePudding user response:

this does not point to the Logger instance in this case it points to the log property of your return object. You can use a reference to this, like in the following example 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();

  • Related