Skip to content

Instantly share code, notes, and snippets.

@awilson28
Last active November 5, 2016 03:39
Show Gist options
  • Save awilson28/b8984c1d246c2c62713f to your computer and use it in GitHub Desktop.
Save awilson28/b8984c1d246c2c62713f to your computer and use it in GitHub Desktop.
function Vehicle (type){
  this.type = type || 'car'; 
}
console.log(Vehicle.prototype)  // {} a.k.a empty object
Vehicle.prototype.getType = function(){
  return this.type; 
}
console.log(Vehicle.prototype)  // {getType: [Function]} a.k.a an object with one method defined on its prototype object
function Car () {}
console.log(Car.prototype)  // {}

function inherit(C, P){
  C.prototype =  new P();  //this is where the inheritance occurs 
}

inherit(Car, Vehicle)
console.log(Vehicle.prototype)  // {getType: [Function]}
console.log(Car.prototype)      // {type: 'car'}

var sedan = new Car(); 
//sedan does not have a type property nor does it have a getType method, so it looks up the _proto_
//chain to grab these properties from Vehicle
console.log(sedan.getType())    // 'car'

console.log(sedan.hasOwnProperty('type'))   // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment