Understanding classes in javascript

ES6 introduced classes, it’s often used by front-end libraries such as react, in simple term classes are special kind of functions.

function Car(type,brand){
this.type= type;
this.brand = brand;
}
Car.prototype.getDescription = function(){


This content originally appeared on DEV Community and was authored by Heru Hartanto

ES6 introduced classes, it's often used by front-end libraries such as react, in simple term classes are special kind of functions.

function Car(type,brand){
    this.type= type;
    this.brand = brand;
}
Car.prototype.getDescription = function(){
    console.log(`This car is ${this.type} from brand ${this.brand}`)
}

let newCar = new Car('Jazz','Honda')
newCar.getDescription()

Before classes introduce in ES6, the best way to create an object template was using functions, but now we can create an object template using classes.

As above example can be rewritten with class in following way

class Car{
    constructor(type,brand){
        this.type= type;
        this.brand = brand;
    }
    getDescription=()=>{
        console.log(`This car is ${this.type} from brand ${this.brand}`)
    }
}
let newCar = new Car('Jazz','Honda')
newCar.getDescription()

Now we have more better approach for creating object template in the form of classes.

The difference between functions and classes in simple are:

  • classes are blueprint for javascript object
  • function are the way to package reusable code

Therefore, when we need to make reusabled block of code that used multiple time, we can package those block inside function, However
when we need to create an object that needs to generated mulitple times then use classes


This content originally appeared on DEV Community and was authored by Heru Hartanto


Print Share Comment Cite Upload Translate Updates
APA

Heru Hartanto | Sciencx (2021-11-28T22:50:24+00:00) Understanding classes in javascript. Retrieved from https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/

MLA
" » Understanding classes in javascript." Heru Hartanto | Sciencx - Sunday November 28, 2021, https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/
HARVARD
Heru Hartanto | Sciencx Sunday November 28, 2021 » Understanding classes in javascript., viewed ,<https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/>
VANCOUVER
Heru Hartanto | Sciencx - » Understanding classes in javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/
CHICAGO
" » Understanding classes in javascript." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/
IEEE
" » Understanding classes in javascript." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/. [Accessed: ]
rf:citation
» Understanding classes in javascript | Heru Hartanto | Sciencx | https://www.scien.cx/2021/11/28/understanding-classes-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.