Thursday, April 18, 2013

Javascript: More on Objects

Plants on the windowsill
Today, I worked a little on objects. In my previous lessons, I've only really worked with numbers and strings. The third data type I'm learning about are objects. They let us code real things and entities by storing all relevant information in one place. To create an object, we use var, followed by the name of the object and =. Each object then starts with {has information inside, and ends with };

// for example:
var Jenny = { };

Each piece of information we include in an object is called a property. Each property has a name, followed by : and then the value of that property. For example, if we want to display my age, we'd type:

age: 18 

we can access properties using two different methods: dot notation and literal notation.

// dot notation
var jenny = new Object();
jenny.username = "cutedork";
jenny.age = 18;

// literal notation
var jenny = {
    username: "cutedork",
    age: 18
};

Song of the day // Static Mind (Girl in a Coma)

No comments:

Post a Comment