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 :)

Thomas
  • 1,678
  • 4
  • 24
  • 47
  • 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. – DCoder Sep 18 '12 at 09: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. – PhonicUK Sep 18 '12 at 09:34
  • @DCoder: you're absoltely right – Thomas Sep 18 '12 at 09:35
  • Try this instead: `lat: (function(v) { return function() { return v; } })(current_point_gps.lat)`. – DCoder Sep 18 '12 at 09:36
  • @PhonicUK: yes result shouldn't be an array but this doesn't solve my problem I think – Thomas Sep 18 '12 at 09:36
  • Assuming you put the exact code above in, your inline functions are missing their semicolons after the return. – PhonicUK Sep 18 '12 at 09:38
  • @DCoder: excellent that works: thanks; Can you add an answer than I can mark it as the right one. Thanks again. – Thomas Sep 18 '12 at 09:39
  • @PhonicUK; these are just code taken from a large program – Thomas Sep 18 '12 at 09:40

3 Answers3

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
Community
  • 1
  • 1
DCoder
  • 12,962
  • 4
  • 40
  • 62
  • 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) – Thomas Sep 18 '12 at 10:35
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;
SvenFinke
  • 1,254
  • 3
  • 15
  • 30
  • 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... – Thomas Sep 18 '12 at 09:33
  • Then try this and remove the (); – SvenFinke Sep 18 '12 at 09:42
  • 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. – SvenFinke Sep 18 '12 at 09:43
  • Thank you for your answer but what do you mean by try this and remove the (); the () after function? – Thomas Sep 18 '12 at 09:47
0
lat : function() { return current_point_gps.lat },
lng : function() { return current_point_gps.lng },

i don't see why this is necessary though

worenga
  • 5,776
  • 2
  • 28
  • 50
  • 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.. – Thomas Sep 18 '12 at 09:34