RSS

Tag Archives: object

Objects and Methods made my head swim – but not any more!

I’ve really been struggling with understanding objects and methods from my work on codecademy. Although it is a great site, because it is crowdsourced teaching, some topics just don’t get the best treatment. It’s not all that common to haveImage someone who knows how to do something well and have them also be able to explain it well to a student (just ask some of my poor victim / students from my first several semesters teaching).

Nevertheless, I trolled through some of the Q&A sessions looking for a good explanation of the material I was struggling with and couldn’t find anything that answered my question very well. What I did find, though, were some decent pieces of ideas that I thought I could combine into a more comprehensive bolus of work that reviewed the topics I just covered, provided some practice working with objects and methods and even a switch statement.

Here’s the setup:

Shermer High school. A couple famous students go there and we can make up Objects representing each of these students using a constructor. Also, each of these students has a brand name jacket that they cherish and these are also created as objects. So the twist is: we’re creating objects(Persons) who have other objects(jackets) as one of their properties. Furthermore, to make this exercise include methods as well, I’ve added ‘wash’ and ‘sew’ methods. Each time one of these methods is carried out, the jackets are changed (e.g. when you wash a jacket, you lose a button. When you sew a jacket, you add a button).

What I wanted to accomplish was to make these objects and methods and learn how to call them so that they carry out their work. Presented below is this program with some annotation and console.log statements that should clear up what’s going on.

Please let me know if this is helpful to anyone – or if this could be cleaned up and done in a more clear and concise way.

// Constructor to make jackets
function Jacket(brand){
switch(brand){
case jordache:
this.make = “Jordache”;
this.buttons = 5;
this.color = “blue”;
break;

case gap:
this.make = “Gap”;
this.buttons = 2;
this.color = “brown”;
break;

case oldNavy:
this.make = “Old Navy”;
this.buttons = 3;
this.color = “white”;
break;
}

}

//constructor to make people
function Person(name,age,brand) {
this.name = name; //unique
this.age = age; //unique
this.brand = new Jacket(brand); //object
this.school = “Shermer High”;    //constant
this.sayHello = function(){
console.log(“Say Hello to ” +this.name+”, student at “+this.school);
};
//method that washes jacket – loses a button each time
this.wash = function(){
console.log(“You just washed “+this.name +”‘s Jacket.”);
this.brand.buttons = this.brand.buttons-1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);
return this.brand.buttons;
};
this.sew = function(){
console.log(“You just mended “+this.name+”‘s jacket.”);
this.brand.buttons = this.brand.buttons +1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);
};

}

// Make three people using a constructor
var geek = new Person(“Anthony Michael Hall”, 14, oldNavy);
var girl = new Person(“Molly Ringwald”, 15, gap);
var caroline = new Person(“Caroline”, 17, jordache);

//now complete with uses of these objects and methods
console.log(“Getting started”);
girl.sayHello();

//console.log(girl.brand.make);
console.log(girl.name+ ” has an awesome “+girl.brand.make +” jacket.”);
girl.wash();
girl.wash();
geek.sayHello();
geek.wash();
girl.sew();
girl.sew();

console.log(“finished”);

 

Tags: , , , , , , , , ,