1. Class
The
class
is a special type of function used for creating multiple objects.1.1. Constructor Method
The constructor method is a special method of a class for creating and initializing an object of that class.
Syntax:
class MyClass {
constructor(property1, property2) {
this.property1 = property1;
this.property2 = property2;
}
method1() { ... }
method2() { ... }
}
1.1.1 Creating a Single Object
Syntax :
class MyClass {
constructor(property1, property2) {
this.property1 = property1;
this.property2 = property2;
}
method1() { ... }
method2() { ... }
}
let myObject = new MyClass(property1, property2);
Example :
JAVASCRIPT
1.1.2 Creating Multiple Objects
JAVASCRIPT
1.2 Prototype property of a Class
JAVASCRIPT
1.3 Prototype of an Instance
The Instance Prototype refers to the prototype object of the constructor function.
JAVASCRIPT
Note
The Type of a class is a function
2.Inheritance in JS Classes
The Inheritance is a mechanism by which a class inherits methods and properties from another class.
2.1 Extends
The
extends
keyword is used to inherit the methods and properties of the superclass.2.2 Super
Calling
super()
makes sure that SuperClass constructor() gets called and initializes the instance.Syntax :
class SuperClass {
}
class SubClass extends SuperClass{
constructor(property1, property2){
super(property1);
this.property2 = property2;
}
method1() { }
}
let myObject = new SubClass(property1, property2);
Here,
SubClass
inherits methods and properties from a SuperClass
.2.3 Method Overriding
Here the constructor method is overridden. If we write the
SuperClass
methods in SubClass
, it is called method overriding.Syntax :
class SuperClass {
}
class SubClass extends SuperClass{
constructor(property1, property2){
super(property1);
this.property2 = property2;
}
}
let myObject = new SubClass(property1, property2);
Example :
JAVASCRIPT
Try out creating superclass and subclass with multiple objects in the JavaScript Code Playground.
3.this in classes
3.1 Super Class
In class,
this
refers to the instance object.JAVASCRIPT
Here
this
refers to the animal1
.3.2 Sub Class
JAVASCRIPT
HERE IS THE EXAPMES HOW WE CAN USE CLASES
Submarine
Given two numbers totalTorpedos , torpedosFired as inputs, write a
super class Submarine with property and methods as below,
Property Description
isSubmerged
It should contain a boolean value to indicate
whether the submarine is submerged or not.
Method Description
dive
When this method is called, it should set the
value of isSubmerged to true and log
"Submarine Submerged" text in the console.
surface
When this method is called, it should set the
value of isSubmerged to false and log
"Submarine Surfaced" text in the console.
Add a sub class WeaponUnit which extends to Submarine with
the below properties and methods,
Property Description
torpedos
It should contain the totalTorpedos
loaded.
torpedosLaunched It should contain the torpedosFired .
Method Description
fireTorpedos
When this method is called, it should decrease
the totalTorpedos by torpedosFired and
log the number of torpedos fired and left, as
shown in the sample outputs.
You are given an incomplete Mobile class.
A Mobile object created using the Mobile class should have the
properties like brand , ram , batteryStatus , isCallInProgress ,
and song .
Implement the Mobile class to initialize the mentioned properties
and add the following methods,
- playMusic When this method is executed, it should log a text with the song , as shown in the sample output
- stopMusic When this method is executed, it should log "Music Stopped"
- fullCharge When this method is executed, If the batteryStatus is 100, it should set the value of batteryStatus to 100 and log "Mobile Fully Charged" If the batteryStatus is already 100, it should log "Mobile Already Fully Charged"
- makeCall When this method is executed, It should set the value of isCallInProgress to true It should log "Calling..."
- endCall When this method is executed, If isCallInProgress is false, it should log "No Ongoing Call to End" If isCallInProgress is true, it should set the value of isCallInProgress to false and log "Call Ended
....