Created
January 16, 2015 17:47
-
-
Save awilson28/8738fa9f549fb231d7ab 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
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