Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:32:06+00:00 2026-06-12T04:32:06+00:00

Javascript Noob calling for duty! I would like to write the code for the

  • 0

Javascript Noob calling for duty!

I would like to write the code for the following:

var p1 = new LatLon(userLat, userLon);                                                    
var p2 = new LatLon(destLat, destLon);  
var dist = p1.distanceTo(p2);    

which means I need to create a Javascript struct of sorts that will create a position and then also be able to take a second position as an argument. I DO NOT need the code to calculate the distance. I just need to figure out how to create the object and allow it to accept the arguments. Here is my pseudo code/ failed attempt:

function LatLon(userLat, userLon) {
    var pos = {
      'lat': userLat, 'lon': userLon, 'distanceTo': distanceBetweenTwoPos(pos, pos2);
    }
    return pos;
}

function distanceBetweenTwoPos(pos, pos2){
    // here I will do my own calculations. 
}

Any help on getting something similar to the above, yet functional?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:32:07+00:00Added an answer on June 12, 2026 at 4:32 am

    Using the typical prototypal inheritance approach, it could look like this.

    function LatLon(userLat, userLon) {
        this.lat = userLat;
        this.lon = userLon;
    }
    
    LatLon.prototype.distanceTo = function(pos2){
        console.log(this.lat, pos2.lat);
        console.log(this.lon, pos2.lon);
        // return the result
    }
    

    Inside the LatLon constructor, because you’re calling it with new, this is a reference to the object being constructed, return statement is needed; it’ll return the new object.

    Inside distanceTo, this will be a reference to the object that called the method, so in your code, it would be a reference to the p1 object.

    And of course p2 will be passed as an argument.

    var p1 = new LatLon(userLat, userLon);
    var p2 = new LatLon(destLat, destLon);  
    var dist = p1.distanceTo(p2); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a JavaScript noob and would like to know if there's a better way
Ok im new to javascript and Unity so this is clearly a very noob
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
May sound like a total noob question but i'm looking for a javascript (and
I'm a noob to JSON, and would like to do a foreach through all
First of all, I'm a JavaScript noob and even noober in Backbone.js. I'm following
i'm new here & i'm a noob with javascript, so please, be gentle &
Javascript noob question - which one of the following is best practice and performance
I would like to build a javascript so a user can choose only one
Probably a very basic question from a confused javascript noob... Why do var hasthisvalue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.