Skip to content

Instantly share code, notes, and snippets.

@nickcooley
Created September 9, 2011 16:03
Show Gist options
  • Save nickcooley/1206608 to your computer and use it in GitHub Desktop.
Save nickcooley/1206608 to your computer and use it in GitHub Desktop.
// 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:
logCar({ color: 'blue', make: 'BMW' });
// Example call for OO version:
(new Car('Ferrari', 'red')).log();
var Car = function(make, color){
this.color = color;
this.make = model
}
Car.prototype.log = function(){
console.log("I'm a " + this.color + ' ' + this.make);
}
var Car = {
color:'blue',
make: 'BMW',
logCar: function(){
console.log("I'm a " + this.color + ' ' + this.make);
}
}
function logCar(car){
if(car.hasOwnProperty('color') && car.hasOwnProperty('make'))
console.log("I'm a " + car.color + ' ' + car.make);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment