Jump to content

Is this an array?


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 post
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); Edited by ShadowMage
Link to post
Share on other sites
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...