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

276
Visualizações
how does $: in Svelte works

was reading a svelte tutorial, it's mentioned that $: this is regular in JavaScript, but don't understand it.

let count = 0;
$: doubled = count * 2; 

how would you do this in vanilla JavaScript. checking for code that has been change

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I assume you mean this docs - https://svelte.dev/docs#component-format-script-3-$-marks-a-statement-as-reactive

In this case I think they are talking about programming language syntax feature called "label". In case of JS you may read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

It is needed to look at Svelte source code attentively, but I guess they use this valid JS syntax pattern $: to mark top-level statements and allow them later to parse and pre-process.

So it's not a black JS magic, but a convention only in Svelte "world".

Update:

I think it used somewhere here in source code - https://github.com/sveltejs/svelte/blob/0d017f482016caa51d34918f79dc0b83f0428fd7/src/compiler/compile/Component.ts#L651

If you want to follow the logic, look at usage of "LabeledStatement".

about 4 years ago · Juan Pablo Isaza Relatório

0

how would you do this in vanilla JavaScript. checking for code that has been change

There are various approaches to this. E.g. dirty checking by comparing previous values with current values and thus determining changes. This approach has some downsides. Among others, data might be duplicated and performance can degrade for larger amounts of data because of all the necessary comparisons.

Another method would be to make dependencies explicit and only update when one of the dependencies changes. Svelte generates the code necessary for that automatically but it can be done manually as well. If you look at generated code you will find that variables get marked as invalidated which triggers updates to the dependent variables.

E.g. in a click handler that increments a count variable you get:

const click_handler = () => $$invalidate(0, count++, count);

For the reactive statement, the code is only run if the count has been changed:

$$self.$$.update = () => {
    if ($$self.$$.dirty & /*count*/ 1) {
        $: doubled = count * 2;
    }
};

This code is optimized for performance, though. It uses array indexes for faster data access and more lean code. For code intended to be written and read by humans, you might want to not do that. Generally, I would not recommend doing this manually either way; it is more verbose and you have to make sure to always specify dependencies correctly for it to work.

about 4 years ago · Juan Pablo Isaza Relatório

0

The $: is special Svelte syntax and doesn't "work" in vanilla javascript.

The syntax is compatible with vanilla javascript (it's a labeled statement), therefor tooling like javascript parsers and highlighters don't break.

But vanilla javascript doesn't have any reactivity built-in, so it behaves very different.
(It ignores the $:)

Listening for changes in primitive variables can't be done in vanilla.
Lots of workarounds are available.

about 4 years ago · Juan Pablo Isaza Relatório
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