Intro to Javascript Objects

I have recently signed up for a class in Web Analytics at UBC. I think it will greatly complement my knowledge in usability design and will improve how I produce websites in the future. I haven’t really had much time to study user behavior in the past aside from the usual A/B testing so it will be a welcome change to be able to delve into the analysis of the user and how my websites are being consumed.

I have also made great improvements in my javascripting, having completed a course last year on the basics. I’m also finishing up on the Javascript component at Codecademy (http://www.codecademy.com/). I’m much more confident in my Javascript skills now and am consistently putting it into practice in whatever project I may be working on.

Here’s a snippet of what I have learned so far:

Creating Objects in Literal notation

var person = {
name: 'Ryan', // note the comma
weight: 150 // no comma after the last key-value pair
};

Below is the same but using an instance

var person = new Object();
person.name = 'Ryan';
person.weight = 150;

Now you can access the object’s properties by just stating

var weight1 = person.weight;
var weight2 = person['weight']; //this is an alternate way

You can also create a constructor to make it easier to create new objects

function person(name,weight)
{
this.name=name;
this.weight=weight;
}

And then afterwards, create new objects just by using the constructor

var myFriend=new person("John",150);

That is the basics of Javascript objects . . . I will be sharing more about Javascript and jquery on later posts.

Leave a Reply