Thursday, April 25, 2013

Javascript: Objects II in Review


01/ 04
Instructions: Examine the languages object. Three properties are strings, whereas one is a number. Use a for-in loop to print out the three ways to say hello. In the loop, you should check to see if the property value is a string so you don't accidentally print a number.
    HINT: Use an if statement in combination with the typeof operator to figure out whether or not something is a "string". If it's a "string", then print it!

Make sure you're checking the property value (e.g., "Hello!") and no the property name (e.g., english). Recall that if we save a property name to a variable, we can access the value associated with that name using bracket notation.

Recall the for-in loop:

for(var x in obj) {
executeSomething();
}

/* This will go through all properties of obj one by one and assign the property name on each run of the loop. */

var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var x in languages) {
if (typeof languages[x]);
}
}

02/ 04
Instructions: Add the sayHello method to the Dog class by extending its prototype. sayHello should print to the console: "Hello this is a [breed] dog", where [breed] is the dog's breed.
    HINT: recall how we previously added a method to the Dog class:
Dog.prototype.bark = function()
{
    console.log("Woof");
}; 

To access a dog's breed from within the method, use this.breed.

function Dog (breed) {
    this.breed = breed;
};

// add the sayHello method to the Dog class
// so all dogs now can say hello
Dog.prototype.sayHello = function()
{
    console.log("Hello this is a " + this.breed + " dog");
};

var yourDog = new Dog("golden retriever");
yourDog.sayHello();

var myDog = new Dog("dachshund");
myDog.sayHello();

03/ 04 
Instructions: Let's first see what type Object.prototype is. Do this in line 2 and save it into prototypeType. If all goes well, you should realize that Object.prototype itself is an object! And since all objects have the hasOwnProperty method, it's pretty easy to check if hasOwnProperty comes from Object.prototype.
    HINT: to see what type Object.prototype is, we should use typeof Object.prototype. The property we want to check for is actually "hasOwnProperty", so line 6 should look like: Object.prototype.hasOwnProperty("hasOwnProperty").

// what is this "Object.prototype" anyway...?
var prototypeType = typeof Object.prototype; 
console.log(prototypeType);

// now let's examine it!
var hasOwn = Object.prototype.hasOwnProperty("hasOwnProperty"); 
console.log(hasOwn);

04/ 04 
Instructions: Modify the StudentReport class so that no grades will be printed to the console in the for-in loop. However, getGPA should still function properly in the last line.
    HINT: You should be changing public variables ( this.grade ) to private variables ( var grade ). If we want to getGPA to be able to be called from outside this class, should we change it to be private? You should find yourself needing to modify getGPA itself. this.grade1 will not be available if you did not declare it previously.

function StudentReport() {
    var grade1 = 4;
    var grade2 = 2;
    var grade3 = 1;
    this.getGPA = function() {
        return ( grade1 + grade2 + grade3 ) / 3;
    };
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {
    if(typeof myStudentReport[x] !== "function") {
        console.log("Muahaha! "+myStudentReport[x]);
    }
}

console.log("Your overall GPA is "+myStudentReport.getGPA());







No comments:

Post a Comment