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
Last active
November 5, 2016 03:39
-
-
Save awilson28/b8984c1d246c2c62713f to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment