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

88
Visualizações
How to set instance variables in Javascript using 'this'?

How do I set the class properties of the class below? The latitude and longitude of the class is still undefined after the scope of setLatLong(). I have tried to make variable inside the setLatLong()-function and returned them, but apparently I can't figure out how variables and scopes work in Javascript.

To those who want to try the link, replace this.street_name with 'hvid*' and this.zip_code with '2400'.

import React from "react";

class Place {
  street_name;
  zip_code;
  latitude;
  longitude;
  constructor(zip_code, street_name, latitude = 0, longitude = 0) {
    this.zip_code = zip_code;
    this.street_name = street_name;
    this.setLatLong(latitude, longitude);
  }

  setLatLong(latitude, longitude) {
    const url = `https://api.dataforsyningen.dk/adgangsadresser?q=${this.street_name}&postnr=${this.zip_code}&struktur=mini`
    if (latitude === 0 && longitude === 0) {
      fetch(url)
        .then(response => response.json())
        .then(json => {
          let item = json[Math.floor(Math.random() * json.length)];
          this.latitude = item.y;
          this.longitude = item.x;
        })
    }
  }
}

export default Place;
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