Advanced Javascript: Objects, Arrays, and Array-Like objects
objects
and arrays
are both incredibly useful. They’re also incredibly easy to confuse with each other. Mix in a few objects that look like arrays and you’ve got a recipe for confusion!
We’re going to see what the differences between objects and arrays are, how to work with some of the common array-like objects, and how to get the most performance out of each.
What Objects Are
A javascript object is a basic data structure:
2 var basicObj = {}; // an empty object
3 // {} is a shortcut for "new Object()"
4
5 basicObj.suprise= "cake!";
6
7 basicObj['suprise']; // returns "cake!"
Using {}
instead of new Object();
is know as “Object Literal” syntax.
2
3 favoriteFood: "pizza",
4
5 add: function(a, b){
6
7 return a + b;
8 }
9 };
10
11 fancyObj.add(2,3); // returns 5
12
13 fancyObj['add'](2,3); // ditto.
As you can see, and probably already knew, properties can be accessed a couple of different ways. However, it’s an important point that we’ll come back to in a minute.
Everything in javascript is an object
. Everything. Arrays
, functions
, even numbers
! Because of this, you can do some really interesting things, such as modifying the prototypes
of Objects, Arrays, etc.
2 // an example of something you probably shouldn't do. Ever. Seriously.
3
4 Number.prototype.addto = function(x){
5
6 return this + x;
7
8 }
9
10 (8).addto(9); // returns 17
11
12 // other variations:
13
14 8.addto(9);
15 // gives a syntax error, because the dot is assumed to be a decimal point
16
17 8['addto'](9);
18 // works but is kind of ugly compared to the first method
19
20 var eight = 8;
21 eight.addto(9); // works
What Arrays Are
Javascript arrays are a type of object
used for storing multiple values in a single variable. Each value gets numeric index and may be any data type.
2
3 arr[0] = "cat";
4 arr[1] = "mouse";
See how that syntax is so similar to the syntax used for setting object properties? In fact, the only difference is that objects use a string while arrays use a number. This is why arrays get confused with objects so often.
Length
Arrays have a length
property that tells how many items are in the array and is automatically updated when you add or remove items to the array.
2
3 arr[0] = "cat"; // this adds to the array
4 arr[1] = "mouse"; // this adds to the array
5 arr["favoriteFood"] = "pizza"; // this DOES NOT add to the array
6 // setting a string parameter adds to the underlying object
7
8 arr.length; // returns 2, not 3
The length property is only modified when you add an item to the array, not the underlying object.
The length
is always 1 higher than the highest index, even if there are actually fewer items in the array.
This is somewhat counter-intuitive. PHP does more what you would expect:
2
3 arr = array();
4
5 arr[100] = "php behaves differently";
6
7 sizeof(arr); // returns 1 in PHP
8
9 ?>
You can manually set the length
also. Setting it to 0 is a simple way to empty an array.
In addition to this length property, arrays have lots of nifty built in functions such as push()
, pop()
, sort()
, slice()
, splice()
, and more. This is what sets them apart from Array-Like Objects.
Array-like Objects
Array-like objects look like arrays. They have various numbered elements and a length property. But that’s where the similarity stops. Array-like objects do not have any of Array’s functions, and for-in loops don’t even work!
You’ll come across these more often than you might expect. A common one is the arguments
variable that is present inside of every js function.
Also included in the category are the HTML node sets returned by document.getElementsByTagName()
, document.forms
, and basically every other DOM method and property that gives a list of items.
2 document.forms.length; // returns 1;
3
4 document.forms[0]; // returns a form element.
5
6 document.forms.join(", "); // throws a type error. this is not an array.
7
8 typeof document.forms; // returns "object"
Did you know you can send any number of parameters you want to a javascript function? They’re all stored in an array-like object named arguments
.
2 function takesTwoParams(a, b){
3
4 // arguments is an array-like variable inside of all functions
5 // arguments.length works great
6
7 alert ("you gave me "+arguments.length+" parameters");
8
9 for(i=0; i< arguments.length; i++){
10
11 alert("parameter " + i + " = " + arguments[i]);
12
13 }
14 }
15
16 takesTwoParams("one","two","three");
17 // alerts "you gave me 3 parameter",
18 // then "parameter 0 = one"
19 // etc.
This works great. But that’s about as far as you can go with array-like objects. The flowing example does not work:
2 function takesTwoParams(a, b){
3
4 alert(" your parameters were " + arguments.join(", "));
5 // throws a type error because arguments.join doesn't exist
6 }
So what can you do?
Well you could make your own join()
function, but that adds a lot of unnecessary overhead to your code because it has to loop over everything. If only there were a quick way to get an array out of an array like object…
It turns out there is.
The array functions can be called on non-array objects as long as you know where to find the function (usually they’re attached to the array, but this isn’t an array remember
Prototype
to the win:
Let’s take a look at that a bit more in-depth:
Array
: This object is the original array that all other arrays inherit their properties from.
Array.prototype
:This gives us access to all the methods properties that each array inherits
Array.prototype.slice
: The original slice method that is given to all arrays via the prototype chain. We can’t call it directly though, because when it runs internally, it looks at the this
keyword, and calling it here would make this
point to Array
, not our arguments
variable.
Array.prototype.slice.call()
: call()
and apply()
are prototype methods of the Function
object, meaning that they can be called on every function in javascript. These allow you to change what the this
variable points to inside a given function.
And finally, you get a regular array
back! This works because javascript returns a new object of type Array rather than whatever you gave it. This causes a lot of headaches for a few people who are trying to make subclasses of Array, but it’s very handy in our case!
Gotchas
First, in Internet Explorer, DOM NodeLists
are not considered to be javascript objects, so you cannot call Array.prototype.slice
on them. If you want an array, you’ll have to loop through it the old fashioned way. Or use a hybrid function that tries it the fast way first, then the slow way if that doesn’t work.
See an example here: http://nfriedly.com/demos/ie-nodelist-to-array.
Second, arrays are objects, so you can do this, but it can get you some serious inconsistencies:
2 arr = [];
3
4 arr[0] = "first element"; // adds item to the array
5
6 arr.length; // returns 1
7
8 arr.two = "second element"; // adds an item to the underlying object that array is built on top of.
9
10 arr.length; // still returns 1 !
11
12 // BUT...
13 for(i in arr){
14
15 // this will hit both 0 and "two"
16
17 }
A better solution: wrap arrays in an object if you need both worlds
This is basically a less efficient method of the array subclassing links I mentioned above. While less efficient, it has the advantage of being simple and reliable.
That said, I wouldn’t recommend that you use this in most cases due to issues with speed and extra code requirements. It’s provided here as an example.
Now that your array is (somewhat) protected on the inside, you can loop through it’s items with forEach()
and know that they will match it’s length. You can also add arbitrary properties to ArrayContainer
or mySuperDooperArray
and they won’t get pulled into your forEach()
loop.
This example could be extended to completely protect the array if the need arose.
An Even Better Solution: Hire a javascript expert.
nFriedly Web Development is a top ranked Javascript and AJAX ninja with an extensive portfolio of proven results. I can bring your project to life and make it run faster than you ever imagined. Get in touch with me or get a free instant estimate for new projects.