Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Difference in function declaration within objects JavaScript

What's the difference in declaring functions like this in JavaScript

const sub = {
  _unsubscribed: false,
  unsubscribe() {
    this._unsubscribed = true
  },
  next(value) {
    if (this._unsubscribed) return;
    if (subscriber instanceof Function) return subscriber(value);
    return subscriber.next ? subscriber.next(value) : null;
  },
  error(error) {
    if (this._unsubscribed) return;
    this._unsubscribed = true;
    return subscriber.error ? subscriber.error(error) : null;
  },
  complete() {
    if (this._unsubscribed) return;
    this._unsubscribed = true;
    return subscriber.complete ? subscriber.complete() : null;
  }
}

versus this:

const sub = {
  _unsubscribed: false,
  unsubscribe: () => this._unsubscribed = true,
  next: (value) => {
    if (this._unsubscribed) return;
    if (subscriber instanceof Function) return subscriber(value);
    return subscriber.next ? subscriber.next(value) : null;
  },
  error: (error) => {
    if (this._unsubscribed) return;
    this._unsubscribed = true;
    return subscriber.error ? subscriber.error(error) : null;
  },
  complete: () => {
    if (this._unsubscribed) return;
    this._unsubscribed = true;
    return subscriber.complete ? subscriber.complete() : null;
  }
}

When using the object instance sub to call the functions within itself repeatedly, what are the properties of using the first method versus the second method?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda