Javascript Cheat Sheet Page 3

ADVERTISEMENT

Arrays and Dictionaries
Here’s an example on how to use Array’s and Dictionaries
var
blah =
new
Array();
blah[0]="1";
//1 element
blah[1]="2";
//2 elements
blah[2]= 3;
//3 elements
blah.push(4.01);
// 4 elements
blah.push(function()
{console.log("i have
blahed");});
//5 elements
blah["Key"] = "Value";
//Not an element of the array
console.log(blah);
//prints ["1", "2", 3, 4.01, function()]
blah[4].call();
//Prints "i have blahed"
if(blah.length == 5) blah.pop();
console.log(blah.length);
//Prints 4
console.log(blah.toString());// Prints "1,2,3,4.01"
console.log(blah["Key"]);//Prints "Value"
As you can see, no types anywhere, Array contains a collection of different types, and everyone is
happy. It even includes an objection
! The objection here is an anonymous objection. They
(read function)
are essentially delegates. Things to note:
Array implements push and pop operations
All objects implement toString().
Element “Key” does not count to length. It can be used as a Dictionary ADT for KEY VALUE pairs.
(If this means nothing to you, forget it)
The anonymous objection can be called by using call(), since it doesn’t have its own name.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3