Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!