Sunny
Sunny

Reputation: 10105

Different output displayed when displaying objects through Javascript alert() and console.log()

This is possibly very basic but I am missing an explanation. If I alert the LatLngBounds returned by getBounds() method, I see two lat/long values, as expected: ((num1,num2),(num3,num4)). Clearly the getBounds() method returns an object. If I do a console.log of the same returned object, I get the display of the object with properties such as:

 ga: Dg
    b: -87.73588298847653
    d: -87.56422161152341
 ...
 ta: Ig
    b: 41.8883823394486
    d: 41.811660656497494
 ...

QUESTION: a. Does alert() convert the object to a string? I thought otherwise. b. What are these ga, ta? I would have expected some names like sw, ne (for south-west and north-east) followed by lat and long, for example. Is there any meaning associated with these ga, ta, b, d names?

UPDATE: I updated the title to remove reference to Google Maps API because the question and the answer are generic to any Javascript Object. It just happened that I faced it while doing so Map API coding. The answer is worth going through if you were ever wondering why your alert output is not the same as the console.log output for certain objects. Also read

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Upvotes: 2

Views: 96

Answers (1)

Anto Jurković
Anto Jurković

Reputation: 11258

alert() expects a string. If you check bounds object in console.log (expand __proto__ property), you will see that there is also function toString() which is called on alert(bounds).

Properties ga and ta are internal presentation of bounds data which are transformed to sw and ne values. It is not wise to use/manipulate them directly because your code could break down when this internal structure is changed.

Additionally, if you call

console.log('bounds: ' + bounds);

the result will be, for example

bounds: ((-34.028249, 151.157507), (-33.80010128657071, 151.28747820854187)) 

because bounds.toString() method is called due to string concatenation.

Upvotes: 3

Related Questions