Skip to content

Instantly share code, notes, and snippets.

@paulallies
Created September 16, 2011 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulallies/1223005 to your computer and use it in GitHub Desktop.
Save paulallies/1223005 to your computer and use it in GitHub Desktop.
Arrays and Objects
//Bad way to declare objects and arrays
var person = new Object(),
keys = new Array();
//A better way is to use object literals
var person = {},
keys = [];
//Example
var person = {
firstname : "Paul",
lastName : "Allies"
}
var keys = ["123", "345"];
keys[5] = "444";
Y.log("The length of keys: " + keys.length);
//if we use the for loop we may pull out undefined values
for(var i= 0; i < keys.length; i++)
{
Y.log(keys[i]);
}
//always use the for in
for(name in keys)
{
Y.log(keys[name]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment