Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda