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

178
Visualizações
Why does this value return true at the end of the loop, and then false at the start of it?

Consider the following function which should draw an image to the canvas in chunks, cache those chunks, and then on the next loop check against those cached chunks to make sure we don't draw anything more then once. If the console.log confirms that the cached chunk array is never cleared, why is it that when the result of pixels.data == cachedImage[y][x] is printed, it always returns false, but after updating the cache it reports as true?

async function temp() {
   const DIVIDE = 8;
   for(let y = 0; y < DIVIDE; y++) {
       if(cachedImage[y] == undefined) {cachedImage[y] = []; console.log(y);}
       for(let x = 0; x < DIVIDE; x++) {
           let widthChunk = (drawBuffer.width)/DIVIDE;
           let heightChunk = (drawBuffer.height)/DIVIDE;
           let pixels = ctx.getImageData(x*widthChunk,y*heightChunk,widthChunk-1,heightChunk-1);
           let pixeldata = pixels.data;
           debugBox3.innerHTML = (cachedImage[y][x] == pixels.data); // always "false"
           if(pixels.data == cachedImage[y][x]) {
           } else {
               cachedImage[y][x] = pixels.data;
               ctxFinal.putImageData(pixels,x*widthChunk,y*heightChunk)
           }
           debugBox4.innerHTML = (cachedImage[y][x] == pixels.data); // always "true"
       }
   }
}
await temp();

(full file avaliable here)

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

0

Suppose the following code:

let object1 = { key: 'value' }

var object2 = { key: 'value' }

One would think that object1 == object2 is true, but its not. Why? Because object1 and object2 are merely references to objects - to independent objects that is.

However, if we extend the code:

let object1 = { key: 'value' }

var object2 = { key: 'value' }

object1 = object2

Now we have set the variable object1 equal to the reference to object2. We have not copied object2.

Therefore object1 == object2 is now true.

When you say cachedImage[y][x] = pixels.data; you actually make cachedImage[y][x] a reference to pixels.data. Only then is it equal to pixels.data (which is also just a reference).

This is somewhat familiar to pointers in c, see: https://www.sitepoint.com/how-javascript-references-work.

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