ES6 Class in 5 minutes

Hashir Hussain
Sep 7, 2018 · 1 min read

We know the purpose of classes in sense of programming concepts.

In JavaScript class is the makeover upon traditional prototype-inheritance.

ES5 Approach

function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.firstName = function() {
console.log(this.first);
}
Person.prototype.lastName = function() {
console.log(this.last);
}
Person.prototype.fullName = fucntion() {
console.log(this.first + ' ' + this.last);
}
var me = new Person('Hashir','Hussain');
console.log(me.fullName()); //Hashir Hussain

ES6 Approach

class Person() {
constructor(first, last) {
this.first = first;
this.last = last;
}
firstName() {
console.log(this.first);
}
lastName() {
console.log(this.last);
}
fullName() {
console.log(this.first + ' ' + this.last);
}
}
var me = new Person('Hashir','Hussain');
console.log(me.fullName()); //Hashir Hussain

Above code is available here.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade