Jump to content

Is this an array?


sepoto

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...