Last active
January 17, 2020 14:49
-
-
Save StefanK2ff/fe6d1639ec09f7b20f942c8db0a56bff to your computer and use it in GitHub Desktop.
Connection between function upon objects, constructors and extended objects
This file contains hidden or 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 Car(brand, model, seats, passengers) { | |
this.brand = brand; | |
this.model = model; | |
this.seats = seats; | |
this.passengers = passengers; | |
} | |
var bmw = new Car ("BMW", "X5", 5, 0); | |
var lexus = new Car ("Lexus", "L300", 4, 0); | |
Car.prototype.start = function () { | |
console.log(this.brand + " start"); | |
}; | |
//bmw.start(); | |
Car.prototype.embark = function (passengers) { | |
if (passengers + this.passengers <= this.seats) { | |
this.passengers = passengers; | |
} else { | |
console.log("Too many passengers!"); | |
} | |
} | |
Car.prototype.disembark = function (passengers) { | |
if (passengers <= this.passengers) { | |
this.passengers = this.passengers - passengers; | |
} else { | |
console.log("not enough passengers to disembark!"); | |
} | |
} | |
// bmw.__proto__ === Car.prototype; --> true | |
// lexus.__proto__ === Car.prototype; | |
//Extending the car prototype | |
function ECar (brand, model, seats, passengers, plug){ | |
Car.call(this, brand, model, seats, passengers) | |
this.plug = plug | |
} | |
ECar.prototype = Object.create(Car.prototype); // connects .__proto__ from HybridCar to car.prototype | |
ECar.prototype.constructor = ECar; | |
const bmw2 = new ECar("BMW", "i3", 3, 0, "big blue one"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment