JavaScript - Object Oriented Programming Using ES6 - by NC Patro - Codeburst
JavaScript - Object Oriented Programming Using ES6 - by NC Patro - Codeburst
Class is not an object, it’s like blueprint which can generate objects. So, Top highlight
class helps to set the classification of objects with its properties and
capabilities.
class Car {
In the above code snippet, we can see the typeof class Car still return as
function because behind the scene JavaScript still works with function only.
Methods are again functions which attached with instance and all
instance created from the same class will have those methods or actions.
class Meetup {
constructor(name, location) {
this.name = name;
this.location = location;
}
start() {
console.log(this.name + 'Meetup ' + 'is started at ' +
this.location);
}
}
The instance jsMeetup created using new keyword and class Meetup has
been called like function.
we can pass arguments to the class as well like any other function and
behind the scene, it will call to the constructor function for initializing
the instance variables name and location .
you can go through blog JavaScript — All about this and new keyword to
understand in-depth working of this and new keyword.
class Meetup {
constructor(name, location) {
this.name = name;
this.location = location;
}
start() {
console.log(this.name + 'Meetup ' + 'is started at ' +
this.location);
}
static getAddress() {
console.log('Returned Address');
/* this.location will return undefined */
console.log('City: '+ this.location );
}
}
Meetup.admin = "Adam";
Meetup.getMembers = function () {
console.log(Meetup.admin+ ' Returned Members');
}
console.log(Meetup.admin); // Adam
console.log(jsMeetup.admin); // undefined
The instance jsMeetup cannot access the class only methods i.e. static
methods getMembers() and getAddress() . Same with static properties as
We can put label as static in-front of methods and properties inside the
class definition to make it accessible to class only or we can add it later
to the class as well like Meetup.getMembers = function(){...} .
class Meetup {
constructor(name) {
this._name = name;
}
get name() {
// Validation can happen on data
return this._name;
}
set name(val) {
// Validation can happen on data
this._name = val;
}
meetup.name = 'Angular';
console.log("meetup Name: " + meetup.name); // meetup Name: Angular
With getter and setter, will have more control on object properties after
initialization with constructor.
We can do required validation on data within get and set method before
setting or getting the value.
We can see in the above code that the instance property name is _name
but we are using it as meetup.name and its working fine because of getter
and setter methods.
Inheritance in ES6
class Meetup {
We can see that object js is instance of class TechMeet and Meetup both
because class TechMeet extends parent class Meetup .
class Meetup {
constructor() {
console.log("inside Meetup constructor");
}
}
class Meetup {
constructor(organizer) {
this.organizer = organizer;
}
}
class Meetup {
organise() {
console.log('Organising Meetup');
}
static getMeetupFounderDetails() {
console.log("Meetup Founder Details");
}
}
static getMeetupFounderDetails() {
console.log("TechMeet Founder Details");
super.getMeetupFounderDetails();
}
}
js.organise();
/* Output:
Organising TechMeet
Organising Meetup */
TechMeet.getMeetupFounderDetails();
/* Output:
TechMeet Founder Details
Meetup Founder Details */
Child class can access the methods of parent class using super object
like super.organise() .
Similarly static methods of child class can access the static method of
parent class with help of super object.
Summary
Syntax changes in ES6 and new features are helping to write better, cleaner
and less code to achieve the object-oriented concepts in JavaScript. It’s
helping to bridge the gap between prototypal JavaScript and classical
programmers.
If you like this post and it was helpful, please click the clap 👏 button
multiple times to show support, thank you.
1.5K 9
WRITTEN BY
NC Patro Follow
codeburst Follow
React - A simple 鬼灭之刃 剧场版 无限列车 Kbone and WeChat Mini- Javascript reduce
beginning 😀 篇 完整版本 (2020-HD) Program Development function with array of
Deepika Srinivasan in Nerd For
Kimetsu no Yaiba: Mugen Kai Yang
objects
Tech
Ressha-Hen 完整版觀看 Jay Han
電~看电影.
Hening Sunyi