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

341
Visualizações
Listening to changes made to a variable (for real)

I have the following situation:

  • I have two Vanilla JS components.
  • The first component, Alpha, can make changes to a window variable, e.g. window.App.messages.
  • The second component, Beta, needs to execute a given function but only after window.App.messages has changed.

We have the following constraints:

  • [A] We don't want the two components to communicate to each other in any other way other than through window.
  • [B] We don't want Beta to change window.App.messages in any way.
  • [C] window.App.messages should be agnostic with regards to which component is going to set and get its data.

I found a number of relevant Stack Overflow threads, but they all seem to suggest solutions that don't actually work to solve this use case. Specifically:

  • Use Proxy (example 1, example 2). The problem with Proxy is that you then need to listen to changes to the proxy and not the original object. This implementation wouldn't work as it would require Alpha and Beta to communicate directly. It breaks [A].
  • Overwrite setters and getters via defineProperty (example). This solution requires Beta to change the original object and that's not okay. It breaks both [B] and [C].
  • Create the object with custom setters and getters. This too doesn't work since it breaks [C].

What I would need is something like this:

var window.App.messages = ["Hello, world!"];

function logChange(variable) {
  console.log(`New value: ${variable.at(-1)}`)
};

var varListener = Listener(window.App.messages, logChange);

window.app.messages.push("The answer is 42.");

>> New value: The answer is 42.
about 4 years ago · Santiago Trujillo
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