0

What I'm trying to accomplish is calling result.MappingWaypoints[i].lat(); and result.MappingWaypoints[i].lng(); instead of result.MappingWaypoints[i].lat; and result.MappingWaypoints[i].lng.

Is this even possible?

var result = new Array();
    result = {
        'MappingWaypoints': MappingWaypoints,
        'Flightime': flight_time_sec,
        'PhotoArea': {  'SouthWest': MeterstoGPS(starting_photo_area),
                        'NorthEast': MeterstoGPS(end_photo_area)},
    };


 MappingWaypoints.push(
 {
    lat : current_point_gps.lat,
    lng : current_point_gps.lng,
    Radius : 10,
    Altitude : Attitude,
    ClimbRate : ClimbRate,
    DelayTime : DelayTime,
    WP_Event_Channel_Value : 100,
    Heading : 0,
    Speed : Speed,
    CAM_Nick : 0,
    Type : 1,
    Prefix : "P"
 }

Edit: I tried adding :

lat: function(){ return current_point_gps.lat},`


lng : function(){ return current_point_gps.lng},

But that doesn't work => all values of the array have the lat/long value of the first coord

edit2: Thanks to @DCoder for this answer in the comments below: this solved it for me.

lat: (function(v) { return function() { return v; } })(current_point_gps.lat)

I know this might seem to be a strange thing to do but an algorithm i'm using really needs to acces data as a function call because the algo also processes data from the google api where data is only accessible trough the same function call lat() and lng(). THANKS :)

8
  • Unless you create a more complex solution for storage, the answers given so far will blow up once the value of current_point_gps changes. Commented Sep 18, 2012 at 9:33
  • Side note - you're mixing up arrays and objects (which are associative arrays) so don't use var result = new array() when you're treating it as an associative array/object. Commented Sep 18, 2012 at 9:34
  • @DCoder: you're absoltely right Commented Sep 18, 2012 at 9:35
  • Try this instead: lat: (function(v) { return function() { return v; } })(current_point_gps.lat). Commented Sep 18, 2012 at 9:36
  • @PhonicUK: yes result shouldn't be an array but this doesn't solve my problem I think Commented Sep 18, 2012 at 9:36

3 Answers 3

1

Your original approach runs into the problem that is described in this question - due to JS variable scope, all the functions will point to (and return) the same variable.

You need to capture the value you want to return for each property, instead of capturing a variable. The simplest way to do that is a self-executing function:

{
  lat: (function(v) { return function() { return v; } })(current_point_gps.lat),
  lng: (function(v) { return function() { return v; } })(current_point_gps.lng),
  ....
}

If you need a way to also edit the lat property, then you can either add a public property to the object, like this:

{
  lat: function() { return this._lat; },
  _lat: current_point_gps.lat
}

Or you can use something similar to jQuery's "0 arguments means get, 1 argument means set" paradigm and use this:

{
  lat: (function(v) { return function() { 
    if(arguments.length) {
      v = arguments[0];
    } else { 
      return v; 
    };
  })(current_point_gps.lat)
}

obj.lat(); // returns the current value
obj.lat(55); // changes the current value to 55, returns nothing
obj.lat(); // returns 55
Sign up to request clarification or add additional context in comments.

1 Comment

Is it also possible to change this value of lat and lng with or without function once the array is created? (edit: not pushing; i want to change the value of lat/lng because their coord are rotated)
0
lat : function() { return current_point_gps.lat },
lng : function() { return current_point_gps.lng },

i don't see why this is necessary though

1 Comment

should have mentioned that I tried that. I know this seems strange but the reason is that another method absolutely need to access the data as a function..
0

You would need to call them as a function.

lat : function(){ return current_point_gps.lat },
lng : function(){ return current_point_gps.lng },

That should work (not tested ;) ).

But why would you wan't to do that?

Edit:

If you want to pass the function, you should not call the function.

// Pass the Result of a function, the function is called
var foobar = result.MappingWaypoints[i].lng();

// Pass the function itself, the function wont be called
var foobar = result.MappingWaypoints[i].lng;

4 Comments

I should have mentioned that I tried that. I know this seems strange but the reason is that another method absolutely need to access the data as a function...
Then try this and remove the ();
That way the variables become functions. But using lat(); you will pass the result of the function, and that would be a string. Pass lat and then you will pass the function itself.
Thank you for your answer but what do you mean by try this and remove the (); the () after function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.