Creating Objects in JavaScript

Oh, hello. It has been awhile, hasn’t it? Well, I’ve been bouncing around between projects–I tend to do that a lot–and have had long stretches of time where I don’t work on anything–I also do that a lot.

Most recently, my current project has required me to dive into JavaScript; a language I have largely avoided in all my years. But, once you get over the camel case, it’s not so bad. It’s even possible to create objects, in a roundabout, completely non-obvious way. Once I knew what to look for (composite data types), I realized the solution was out there a bit, but since I refer back to my own blog quite a bit when I forget something–and with so long between coding sessions, it’s no wonder–it’s probably a good idea to repeat it here.

var arr = new Array(); // setting up arr to be an array allows us to use the push function on it later
var obj = {
	name: "Dangel", // notice, : is our "assignment operator" and , terminates our line
	age: 25 // the last item doesn't need a ,
}; // don't forget the semi-colon!
arr.push(obj); // pushing will stuff obj into arr
// now, since arr is an array, it needs to be accessed like one
// so, to find the name, it'd be arr[0].name
// there is probably a way around this, but i haven't not needed an array of objects yet :)

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>