Skip to content

Instantly share code, notes, and snippets.

@AkshatJen
Created October 21, 2020 23:50
Show Gist options
  • Save AkshatJen/6a840515e4e0a87573748284457943f4 to your computer and use it in GitHub Desktop.
Save AkshatJen/6a840515e4e0a87573748284457943f4 to your computer and use it in GitHub Desktop.
Javascript Classes under the hood and how it works
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