Created
September 28, 2020 16:15
-
-
Save azamsharp/4c1395638af18efc7db8ebcd94962ac6 to your computer and use it in GitHub Desktop.
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
// Classes in JavaScript | |
function Car(make, model) { | |
this.make = make | |
this.model = model | |
} | |
/* | |
function drive() { | |
console.log("Car is driving") | |
} */ | |
/* | |
function brake() { | |
console.log("Brake car") | |
} */ | |
//Car.prototype.whatevernameoffunction = function | |
//Car.prototype.drive = drive | |
//Car.prototype.brake = brake | |
Car.prototype.drive = function() { // anonymous functions | |
console.log("Car is driving") | |
} | |
Car.prototype.brake = function() { | |
console.log("Brake the car...") | |
} | |
Car.prototype.changeGear = function(gearNo) { | |
console.log("Gear changed to " + gearNo) | |
} | |
//drive() | |
//brake() | |
let car = new Car("Honda", "Accord") | |
car.drive() | |
car.brake() | |
car.changeGear(2) | |
car.isElectric = false // dynamic property added to car object | |
//let carObject = new Object() | |
let carObject = {} | |
carObject.make = "Honda" | |
carObject.model = "Accord" | |
carObject.drive = drive | |
//console.log(carObject) | |
// MODERN JAVASCRIPT | |
class ElectricCar { | |
constructor(make, model) { | |
this.make = make | |
this.model = model | |
} | |
drive() { | |
console.log("drive") | |
} | |
brake() { | |
console.log("brake") | |
} | |
} | |
let ec = new ElectricCar("Tesla","Model 3") | |
ec.drive() | |
ec.brake() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment