Created
September 16, 2011 20:07
-
-
Save paulallies/1223005 to your computer and use it in GitHub Desktop.
Arrays and Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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