Pages

Wednesday, 6 October 2021

Classes Inheritance and Encapsulation in JavaScript

//****** Classes Inheritance and Encapsulation **********
/*** Inheritance b/w classes and constructor functions ***/
// Contructor Function1
const Employee = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Employee.prototype.getFullName = function() {
console.log(`${this.firstName} ${this.lastName}`);
}
// Contructor Function2
const Developer = function(firstName, lastName, level) {
// this.firstName = firstName;
// this.lastName = lastName;
Employee.call(this, firstName, lastName); // To reuse employee
this.level = level;
}
// To inherit the functions present in Employee to Developer
Developer.prototype = Object.create(Employee.prototype);
// Developer.prototype = Employee.prototype; // won't work
Developer.prototype.introduce = function() {
console.log(`This is ${this.firstName} and I am ${this.level} Developer.`);
}
const developerSrinu = new Developer('Srinu', 'SFDC', 'Senior');
developerSrinu.introduce();
developerSrinu.getFullName();
console.log(developerSrinu.__proto__);
console.log(developerSrinu.__proto__.__proto__);
console.dir(Developer.prototype.constructor);
Developer.prototype.constructor = Developer;
console.dir(Developer.prototype.constructor);
/*** Inheritance b/w ES6 classes ***/
class EmployeeCls {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getFullName() {
console.log(`${this.firstName} ${this.lastName}`);
}
}
class DeveloperCls extends EmployeeCls {
constructor(firstName, lastName, level) {
// No need to use call here use super
super(firstName, lastName); // this should be the first statement
this.level = level;
}
introduce() {
console.log(`This is ${this.firstName} and I am ${this.level} Developer.`);
}
// Overriding the parent method
getFullName() {
console.log(`${this.firstName} ${this.lastName} is my full name.`);
}
}
const khem = new DeveloperCls('Khem', 'Bhole', 'Team Lead');
// Check khem in console
khem.introduce();
khem.getFullName(); // this is from the parent
/*** Inheritance b/w classes, Object.create ***/
const EmployeeProto = {
init(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
},
getFullName() {
console.log(`${this.firstName} ${this.lastName} is my full name.`);
}
};
const DeveloperProto = Object.create(EmployeeProto);
DeveloperProto.init = function(firstName, lastName, level) {
// To reuse the parent contructor
EmployeeProto.init.call(this, firstName, lastName);
this.level = level;
};
DeveloperProto.introduce = function() {
console.log(`This is ${this.firstName} and I am ${this.level} Developer.`);
};
const miguel = Object.create(DeveloperProto);
miguel.init('Miguel', 'M', 'Senior');
miguel.introduce();
miguel.getFullName(); // from the parent proto
/*** ES6 Class Example ***/
class Account {
// Public Fields
locale = navigator.language;
// Private Fields
#transactions = [];
#mobileNumber;
constructor(fullName, mobileNumber) {
this.fullName = fullName;
this.#mobileNumber = mobileNumber;
// this.transactions = [];
// this.locale = navigator.language;
console.log(`Congratulations!!! your account is opened.`);
}
// Public Methods
deposit(amount) {
this.#transactions.push(amount);
// To chain the methods
return this;
}
withdraw(amount) {
this.deposit(-amount);
// To chain the methods
return this;
}
getTransactions() {
return this.#transactions;
}
requestLoan(amount) {
if(this.approveLoan(true)) {
this.deposit(amount);
console.log('Loan is approved');
}
// To chain the methods
return this;
}
// Private Methods
#approveLoan(isTrue) {
return isTrue;
}
}
const acc1 = new Account('Gauri C', '878898889',);
// acc1.transactions.push(900);
acc1.deposit(900);
// acc1.transactions.push(-400);
acc1.withdraw(400);
console.log(acc1);
// acc1.approveLoan(true); // should not be accessible
// console.log(acc1.mobileNumber); // should not be accessible
// console.log(acc1.#mobileNumber); // Private field '#mobileNumber' must be declared in an enclosing class
// acc1.#approveLoan(true); // Private field '#approveLoan' must be declared in an enclosing class
//**** Encapsulation: protecting properties and methods ***
// Real private and other features are not available in javaScript
/*** Chaining the methods ***/
acc1.deposit(800).deposit(900).withdraw(-200);
console.log(acc1.getTransactions());

1 comment: