Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Created June 24, 2012 22:26
Show Gist options
  • Save anthonybrown/2985268 to your computer and use it in GitHub Desktop.
Save anthonybrown/2985268 to your computer and use it in GitHub Desktop.
Just another faux class
// A Car 'class'
function Car1(model) {
this.model = model;
this.color = 'silver';
this.year = '2012';
this.getInfo = function() {
return this.model + ' '+ this.year + ' ' + this.color;
};
}
var myCar = new Car1('chevy');
myCar.year = '2003';
console.log(myCar.getInfo());
// another way of writing the function
function Car2( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
}
// We are going to use the Object.prototype.newMethod rather than
// Object.prototype so as to avoid redefining the prototype object
Car2.prototype.toString = function() {
return this.model + ' has done ' + this.miles + ' miles';
};
// process.memoryUsage();
var civic = new Car2('Honda Civic', 2009, 20000 );
var toyota = new Car2( 'Toyota Tundra', 2000, 300000 );
console.log(civic.toString());
console.log(toyota.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment