Created
April 7, 2011 12:50
-
-
Save tacitphoenix/907715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 1 - OO || !OO | |
// Define a data structure for cars (make and color), and a function | |
// that logs a string like "I'm a red Mercedes" to the console. | |
// Make two versions: a functional version, and a object-oriented version. | |
// Example call for functional version(Couldn't Figure out): | |
var logCar = (function(funcCar){ | |
return function(){ | |
console.log("I'm a " + funcCar.color + " " + funcCar.make); | |
} | |
})(funcCar); | |
logCar({ color: 'blue', make: 'BMW' }); | |
// Example call for OO version: | |
var objCar = function(make, color){ | |
this.make = make; | |
this.color = color; | |
this.log = function(){ | |
console.log("I'm a " + this.color + " " + this.make); | |
} | |
}; | |
(new objCar('Ferrari', 'red')).log(); | |
// 1. Write a class to support the following code: | |
var Person = function(name){ | |
this.name = name; | |
} | |
var thomas = new Person('Thomas'); | |
var amy = new Person('Amy'); | |
console.log(thomas.name);// --> "Thomas" | |
// 2. Add a getName() method to all Person objects, that outputs | |
// the persons name. | |
Person.prototype.getName = function(){ | |
return this.name; | |
} | |
console.log(thomas.getName()); // --> "Thomas" | |
// 3. Write a statement that calls Thomas's getName function, | |
// but returns "Amy". | |
thomas.getName = function(){ | |
return amy.getName(); | |
} | |
console.log(thomas.getName()); // --> "Amy" | |
// 4. Remove the getName() method from all Person objects. | |
delete Person.prototype.getName; | |
console.log(thomas.getName()); // --> "Thomas" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment