Introduction to Objects II

I Review: The story so far…

i. An Objective Review 1/30

var james = {
    job: "programmer",
    married: false
};

function Person(job, married) {
    this.job = job;
    this.married = married;
}

var gabby = new Person("student",true);

ii. Fun with Functions 2/30

function Person(job, married) {
 this.job = job;
 this.married = married;
 this.speak = function(){
 console.log("Hello!");
 };
};
 
var user = new Person("Codecademy Student",false);
user.speak();

iii. Literally Speaking 3/30

var james = {
    job: "programmer",
    married: false,
    speak: function(mood) {
            console.log("Hello, I am feeling" + " " + mood);
    }
};

james.speak("great");
james.speak("just okay");

iv. Can I See Your References? 4/30

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

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer";

// james' second job
james.sayJob();

v. Who’s in Your Bracket? 5/30

var james = {
    job: "programmer",
    married: false
};

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james" 
// using the variable "aProperty"
console.log(james[aProperty]);

II Objects, Objects Everywhere

i. I.D., Please 6/30

var anObj = { job: "I'm an object!" };
var aNumber = 42;
var aString = "I'm a string!";

console.log(typeof anObj); // should print "object"
console.log(typeof aNumber); // should print "number"
console.log(typeof aString); // should print "string"

ii. Know Thyself 7/30

var myObj = {
    name: "Saksham"
};

console.log( myObj.hasOwnProperty('name') ); // should print true
console.log( myObj.hasOwnProperty('nickname') ); // should print false

iii. Dressed to Impress 8/30

 var suitcase = {
    shirt: "Hawaiian"
};
if (suitcase.hasOwnProperty('shorts')){
     console.log(suitcase.shorts);
}

else {
    suitcase.shorts = "Cartoon";
};
console.log(suitcase.shorts);

iv. Getting IN-timate 9/30

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};

for (var property in nyc) {
    console.log(property);
}

v. List ALL the Properties! 10/30

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
for ( property in nyc) {
    console.log(nyc[property]);
};

III You Down With OOP?

i Class is in Session 11/30

function Person(name,age) {
  this.name = name;
  this.age = age;
}

// Let's make bob again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 35);

function Circle(radius) {
    this.radius = radius;
}

ii. Teach Snoopy 12/30

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

// here we make buddy and teach him how to bark
var buddy = new Dog("Golden Retriever");
buddy.bark = function() {
  console.log("Woof");
};
buddy.bark();

// here we make snoopy
var snoopy = new Dog("Beagle");
// we need you to teach snoopy how to bark here
snoopy.bark = function() {
  console.log("Woofoo");
};
// this causes an error, because snoopy doesn't know how to bark!
snoopy.bark();

iii. How do Classes Help Us? 13/30

function Person(name,age) {
  this.name = name;
  this.age = age;
}
// a function that prints the name of any given person
var printPersonName = function (p) {
  console.log(p.name);
};

var bob = new Person("Bob Smith", 30);
printPersonName(bob);

// make a person called me with your name and age
// then use printPersonName to print your name
var me = new Person("Sunakshi", 23);
printPersonName(me);

iv. Prototype to the Rescue 14/30
Save and Submit

v. Prototype Practice 15/30

function Cat(name, breed) {
    this.name = name;
    this.breed = breed;
}

// let's make some cats!
var cheshire = new Cat("Cheshire Cat", "British Shorthair");
var gary = new Cat("Gary", "Domestic Shorthair");

// add a method "meow" to the Cat class that will allow
// all cats to print "Meow!" to the console
Cat.prototype.meow = function () {
    console.log("Meow!");
};
// add code here to make the cats meow!
cheshire.meow();
gary.meow();

IV Inheriting a Fortune

i. It’s All in the Genes 16/30

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
};

Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

// create the sayName method for Animal

// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();

ii. Marching Penguins 17/30

 function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

function Penguin(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
};

Penguin.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};    

var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();

iii. DRY Penguins 18/30

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};


function Penguin(name, numLegs) {
    this.name = name;
    this.numLegs = 2;
}
Penguin.prototype = new Animal();

iv. Black (and White) Penguin Magic 19/30

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};


function Penguin(name, numLegs) {
    this.name = name;
    this.numLegs = 2;
}
Penguin.prototype = new Animal();
var penguin = new Penguin();
penguin.sayName();

v. Penguins, Properties, and the Prototype 20/30

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

function Emperor(name) {
    this.name = name;
}
Emperor.prototype = new Penguin();
var emperor = new Emperor();
console.log(emperor.numLegs);

vi. Up the Food-I-mean-Prototype Chain 21/30

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
    this.isAlive = true;
}
function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}
function Emperor(name) {
    this.name = name;
    this.saying = "Waddle waddle";
}

// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();

var myEmperor = new Emperor("Jules");

console.log(myEmperor.saying); // should print "Waddle waddle"
console.log(myEmperor.numLegs); // should print 2
console.log(myEmperor.isAlive); // should print true

V Privacy Please!

i. Open to the Public 22/30

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
}

var john = new Person("Johnny","Bravo",25);
console.log(john.bankBalance);

ii. Private Variables 23/30

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
}

var john = new Person("Johnny","Bravo",25);
console.log(john.bankBalance);

iii. Accessing Private Variables 24/30

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

var myBalance = john.getBalance();
console.log(myBalance);

iv Private Methods 25/30

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   var returnBalance = function() {
      return bankBalance;
   };
   this.askTeller = function() {
       return returnBalance;
   };
  
}

var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);

v. Passing Arguments 26/30

 
function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.askTeller = function(pass) {
     if (pass == 1234) return bankBalance;
     else return "Wrong password.";
   };
}

var john = new Person('John','Smith',30);
var myBalance = john.askTeller(1234)
    

VI Objects in Review Are Closer Than They Appear

i. Looks For-In To Me 27/30

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

// print hello in the3 different languages
for (var lang in languages) {
    if (typeof languages[lang] === "string") {
        console.log(languages[lang]);
    }
}

ii. Hello? Yes, This is Dog 28/30

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

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();

iii. So Meta I Can’t Take It! 29/30

 
var prototypeType = typeof Object.prototype;
console.log(prototypeType);

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

iv. Private Eye 30/30

 
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());
Advertisements

3 thoughts on “Introduction to Objects II

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s