Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
access class private property inside nested method and function?

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda