Created
October 21, 2020 23:50
-
-
Save AkshatJen/6a840515e4e0a87573748284457943f4 to your computer and use it in GitHub Desktop.
Javascript Classes under the hood and how it works
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(make,mileage){ | |
this.make = make; | |
this.mileage = mileage; | |
} | |
car.prototype.incrementMileage = function(){ | |
this.mileage++; | |
} | |
car.prototype.setMake = function(make){ | |
this.make = make; | |
} | |
console.log(car.prototype) | |
const car1 = new car("Toyota", 1200); | |
console.log(car1.make); | |
console.log(car1.mileage); | |
car1.incrementMileage(); | |
car1.setMake("Tesla"); | |
console.log(car1.make); | |
console.log(car1.mileage); | |
class Car{ | |
constructor(make,mileage){ | |
this.make = make; | |
this.mileage = mileage; | |
} | |
incrementMileage(){ | |
this.mileage++; | |
} | |
setMake(){ | |
this.make = make; | |
} | |
} | |
const car2 = new Car("BMW", 100); | |
console.log(car2.make); | |
console.log(car2.mileage); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment