sepoto Posted October 18, 2010 Share Posted October 18, 2010 var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }Is this an array? If I know exactly what it is I can read documentation about. Can someone please define?Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2010 Share Posted October 18, 2010 That's an object literal. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 18, 2010 Share Posted October 18, 2010 AKA, JavaScript Object Notation ( or JSON ) Link to comment Share on other sites More sharing options...
thescientist Posted October 18, 2010 Share Posted October 18, 2010 That's an object literal.which is a type of array, known as an associative array. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2010 Share Posted October 18, 2010 Javascript doesn't have associative arrays, nor regular arrays technically. Everything in Javascript, including a numerical array, is an object. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 18, 2010 Share Posted October 18, 2010 Javascript doesn't have associative arrays, nor regular arrays technically. Everything in Javascript, including a numerical array, is an object.So can you access the elements of a numeric array using object syntax? Property names can't start with numbers so anArray.2 won't work...in fact it throws a "missing ) after argument list" error.var anArray = new Array(1, 2, 3, 4);alert(anArray.2); Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2010 Share Posted October 18, 2010 Right, variable and property names cannot start with numbers. There are internal mechanisms to match array syntax like arr[0] with the property. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 18, 2010 Share Posted October 18, 2010 Right, variable and property names cannot start with numbers. There are internal mechanisms to match array syntax like arr[0] with the property.In much the same way that obj["count"] is the same as obj.count? Link to comment Share on other sites More sharing options...
jeffman Posted October 18, 2010 Share Posted October 18, 2010 The big difference is that an object created with object notation does not have built-in array methods. For that you need an array object. (Or a bunch of work-arounds.) Link to comment Share on other sites More sharing options...
ShadowMage Posted October 18, 2010 Share Posted October 18, 2010 The big difference is that an object created with object notation does not have built-in array methods. For that you need an array object. (Or a bunch of work-arounds.)Does that include array literals? Ie, arr = [1, 2, 3, 4];Or do those inherit the array methods. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2010 Share Posted October 18, 2010 Yeah, creating an array literal creates a regular array object, with methods like push, pop, etc. Link to comment Share on other sites More sharing options...
boen_robot Posted October 19, 2010 Share Posted October 19, 2010 Does that include array literals? Ie, arr = [1, 2, 3, 4];Or do those inherit the array methods. Yeah, creating an array literal creates a regular array object, with methods like push, pop, etc.... or in other words, the "array literal" is what is commonly reffered to as "syntax sugar" - nothing new happening internally, but is added for convinience and briefness. Therefore:var anArray = [1, 2, 3, 4]; and var anArray = new Array(1, 2, 3, 4); Internally do the same thing - create an array, which is initially filled with four elements with values 1, 2, 3 and 4, accessible from special properties called "index"-es, numbered as 0, 1, 2 and 3, respectively.And just to finish up with the "syntax sugar" thing, an "object literal" is also a syntax sugar, but the equivalent examples then become: var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } and var myOptions = new Object();myOptions.zoom = 8;myOptions.center = latlng;myOptions.mapTypeId = google.maps.MapTypeId.ROADMAP; (the first is faster on most browsers, if not all, because it's done "at once", whereas the second fills the object property by property) Link to comment Share on other sites More sharing options...
ShadowMage Posted October 19, 2010 Share Posted October 19, 2010 Gotcha. Thx for the breakdown, boen_robot. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.