Jump to content

Object/Array Literal from old/bad to new/good


Spunky
 Share

Recommended Posts

Can anyone tell me why the following code breaks when I use the literal formatting for creating arrays and objects?

var map;var bounds = new google.maps.LatLngBounds();var infowindow;var i;(function () {  google.maps.Map.prototype.markers = new Array();      google.maps.Map.prototype.addMarker = function(marker) {    this.markers[this.markers.length] = marker;  };      google.maps.Map.prototype.getMarkers = function() {    return this.markers  };      google.maps.Map.prototype.clearMarkers = function() {    if(infowindow) {      infowindow.close();    }        for(var i=0; i<this.markers.length; i++){      this.markers[i].set_map(null);    }  };})();function initialize() {  firstB = new google.maps.LatLng(123,-123);  secondB = new google.maps.LatLng(123,-123);  bounds.extend(firstB);  bounds.extend(secondB);  var myCoord = new google.maps.LatLng(123,-88.123);  var mapOptions = {    zoom:14,    center:myCoord,    disableDefaultUI:true  }  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);  var styles=[  {    featureType:"road",    elementType:"labels",    stylers:[      {visibility:"off"}    ]  }  ];  map.setOptions({styles: styles});var pinIcon = new google.maps.MarkerImage(    'http://maps.google.com/mapfiles/ms/icons/green-dot.png',    null, /* size is determined at runtime */    null, /* origin is 0,0 */    null, /* anchor is bottom center of the scaled image */    new google.maps.Size(52, 52));   var a = new Array();  var t =  new Object();    t.name = "name"    t.lat = 41.7590721    t.lng = -234    t.address = "234"    a[0] = t;    var t =  new Object();    t.name = "name"    t.lat = 123    t.lng = -123    t.address = "123"    a[1] = t;   for (var i = 0; i < a.length; i++) {        var latlng = new google.maps.LatLng(a[i].lat,a[i].lng);        map.addMarker(createMarker(a[i].name,latlng));     };};  function createMarker(name, latlng) {    var marker = new google.maps.Marker({position: latlng, map: map});    google.maps.event.addListener(marker, "click", function() {      if (infowindow) infowindow.close();      infowindow = new google.maps.InfoWindow({content: name});      infowindow.open(map, marker);    });    return marker;  };google.maps.event.addDomListener(window, 'load', initialize);google.maps.event.addDomListener(window, "resize", function() { google.maps.event.trigger(map, "resize"); map.fitBounds(bounds);});

For example this:

  var a = new Array();  var t =  new Object();    t.name = "name"    t.lat = 41.7590721    t.lng = -234    t.address = "234"    a[0] = t;    var t =  new Object();    t.name = "name"    t.lat = 123    t.lng = -123    t.address = "123"    a[1] = t;

Turns into this:

  var a = [];  var t =  {    name = "name",    t.lat = 41.7590721,    t.lng = -234,    t.address = "234"};    a[0] = t;    var t =  {    name = "name",    lat = 123,    lng = -123,    address = "123",    a[1] = t;};
Edited by Spunky
Link to comment
Share on other sites

name = "name",
Look at the other places where you create objects. You use a colon to define properties, not an equal sign.

 

Blast, you're right. I should've known that. I was even on the w3schools website looking at it. Sheesh. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...