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

240
Visualizações
How do i use this in javascript

I am trying to access the length of an array I made inside a function. How do I use the this to access it?

Here is my code:

let hashtable = new Arrary(50)
// create a hashfunction 
let gethash = (key)=>{
    // make sure all value passed in are strings 
        let keyStr = key.toString();
        let sum = 0
        for (let i = 0; i < keyStr.length; i++) {
            sum =+ keyStr.charAt(i)
        }
        return sum % this.hashtable.length

}

I am learning hashmaps in data structures and algorithms. I would like to use the length of the hash table to ensure that my hash function falls between the size of the array.

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

0

Does it not work without the this keyword?

let hashtable = new Arrary(50)
// create a hashfunction 
let gethash = (key)=>{
    // make sure all value passed in are strings 
        let keyStr = key.toString();
        let sum = 0
        for (let i = 0; i < keyStr.length; i++) {
            sum =+ keyStr.charAt(i)
        }
        return sum % hashtable.length

}

You aren't accessing something bound to a class or object, so you should be able to treat it like a normal variable.


More about the this keyword:

In most cases, the value of this is determined by how a function is called (runtime binding). It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind() method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).

Source

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