/**
* To demonstrate Classes in ES6
*/
//define the class
class Employee{
constructor(firstName, lastName){//this is constructor for class
this.firstName = firstName;
this.lastName = lastName;
}
printName(){//this is method for class
console.log("My Name is :: " + this.firstName+" "+this.lastName);
}
}
//creating object of the class
var employeeJohn = new Employee("John","Kesar");
//print object on console
console.log("employeeJohn ::" + employeeJohn);
console.log(employeeJohn);
//accessing object function
employeeJohn.printName();
//accessing object property
console.log("employeeJohn.firstName :: " + employeeJohn.firstName);
//creating unnamed class
var employeeVar = class{
constructor(firstName,lastName, idNo){//this is constructor for class
this.firstName=firstName;
this.lastName=lastName;
this.idNo=idNo;
}
printPersonId(){
console.log("Person Id :: " + this.idNo);
}
printPersonInfo(){
console.log("Person Id :: "+this.idNo+", Name :: "+this.firstName+" "+this.lastName);
}
}
//creating object of unnamed class
var employeeMohan = new employeeVar("Mohan","Kore","777");
employeeMohan.printPersonInfo();
//assigning another unnamed class to same variable
employeeVar = class{
constructor(firstName,lastName, idNo,role="Software Engineer"){//this is constructor for class
this.firstName=firstName;
this.lastName=lastName;
this.idNo=idNo;
this.role=role;
}
printPersonId(){
console.log("Person Id :: " + this.idNo);
}
printPersonInfo(){
console.log("Person Id :: "+this.idNo+", Name :: "+this.firstName+" "+this.lastName+", role ::"+this.role);
}
}
//creating objects of unnamed classes using same variable employeeVar
var employeeKiran = new employeeVar("Kiran","Kaur","111");
employeeKiran.printPersonInfo();
var employeeNupur = new employeeVar("Nupur","Kaur","222","Lead Developer");
employeeNupur.printPersonInfo();
console.log(employeeNupur);
//getter methods
//{get prop() { ... } }
//{get [expression]() { ... } }
employeeVar = class{
constructor(firstName,lastName, idNo,role="Software Engineer"){//this is constructor for class
this.firstName=firstName;
this.lastName=lastName;
this.idNo=idNo;
this.role=role;
}
get fullName(){//demo for get [expression](), fullName is not property of object
return this.firstName+" " + this.lastName;
}
}
var employeeMihir = new employeeVar("Mihir","Kumar","333","DBA");
console.log("employeeMihir.fullName :: " + employeeMihir.fullName);
console.log("employeeMihir.firstName :: " + employeeMihir.firstName);
console.log("employeeMihir.lastName :: " + employeeMihir.lastName);
//static method
employeeVar = class{
constructor(firstName,lastName, idNo,role="Software Engineer"){//this is constructor for class
this.firstName=firstName;
this.lastName=lastName;
this.idNo=idNo;
this.role=role;
}
static sayHello()
{
return "hey there, hello";
}
}
var employeeMonty = new employeeVar("Monty","pollard","555","Manager");
//static methods are called by class name. calling by object name gives error
console.log("employeeMonty says :: " +employeeVar.sayHello());
//instanceof operator returns if object is of class?
console.log("class of Monty is employeeVar ? " + (employeeMonty instanceof employeeVar));
//class inheritance it can be single level or multilevel
class Shape {
constructor(color){
this.color=color;
}
printShape(){
return this.color;
}
}
class Rectangle extends Shape{
constructor(length, width, color){
super(color);
this.length=length;
this.width=width;
}
printArea(){
return this.length*this.width;
}
printShape(){//method overriding
return "Rectangle having area :: " + this.printArea()+", with color :: "+super.printShape();
}
}
var rectangleRed = new Rectangle(10,20,"red");
console.log("rectangleRed :: "+rectangleRed.printShape());
console.log("rectangleRed instanceof Shape ? " + (rectangleRed instanceof Shape));
console.log("rectangleRed instanceof Rectangle ? " + (rectangleRed instanceof Rectangle));
Output
