Friday, April 19, 2013

Javascript: Intro to Objects II




















I know we've been reviewing objects I again and again these past couple of blog posts, but I think it's safe to say that there's a reason for this perpetual repetition. Codeacademy really wants you to get down the basics before moving into new territories. Last night I attempted to finish the entire course in one-go--and was almost successful in that endeavor. However, I was stumped by the last lesson... probably because my level of understanding was just sufficient in order for me to complete the problems right in front of me, but not enough to truly understand and apply what I had just learned to more complex situations. Oh well. Hopefully after updating this blog with the lessons I covered last night, I'll be somewhat more refreshed (and a gain deeper understanding)!

// literal notation creates a single object
var jenny = {
    job: "student",
    married: false
};

// constructor notation involves defining an object constructor using the function keyword
function Person(job, married) {
    this.job = job;
    this. married = married;
}
// create a "jenny" object using the Person constructor
var jenny = new Person("student", false);

Methods are essentially functions associated with objects, remember?
function someObj() {
this.someMethod = function() {
};
}

// add a speak method to the Person constructor via constructor notation 
function Person(job, married) {
    this.job = job;
    this.married = married;
    this.speak = "Hello!"
}

// add method to object via literal notation
var jenny = {
    job: "student",
    married: false,
    speak: function(mood) {
        console.log("Hello, I am feeling " + mood); 
};

jenny.speak("fantastic");
jenny.speak("meh");


When defining a method for an object, use this.propertyName to reference other properties in that object. When that method is called, this.propertyName will always refer to the most recent value of propertyName.

var jenny = {
    job: "student",
    married: false,
    sayJob: function() {
        console.log("Hi, I work as a " + this.job); 
    }
};

// jenny's first job
jenny.sayJob();

// change jenny's job to "coding wizard with big fluttery beard"
jenny.job = "coding wizard with big fluttery beard";

//jenny's second job:
jenny.sayJob();

Throughout this post, I've been using dot notation to get the value of an object's property:
someObj.propName
However, we can also use bracket notation:
someObj["propName"] 

The advantage of using bracket notation is that we aren't restricted to using only strings in the brackets. We can also use variables whose values are property names:
var someObj = {propName: someValue};
var myProperty = "propName";
someObj[myProperty]

// some Obj[myProperty] = someObj["propName"] 
/* first set aProperty to a string of the first property in jenny (ie. the job property), then print jenny's job using bracket notation and a property */
var aProperty = "job";

console.log(jenny[aProperty]; 
);

No comments:

Post a Comment