You are on page 1of 13

Class

JavaScript classes, introduced in ECMAScript 2015 or ES 6,


Classes are in fact "special functions".
There are two way to define class in JavaScript using class
keyword:-
• Class Declaration
• Class Expression
Class Declaration
class class_name { class Mobile {
constructor ( ) { constructor ( ) {
this.model = ‘Galaxy’;
Properties
}
} show() { return this.model +
Methods “Price 3000”;
} }
}
var nokia = new Mobile( );
Constructor
The constructor method is a special method for creating and initializing an
object created within a class. There can be only one special method with the
name "constructor" in a class.
class Mobile {
class class_name { constructor ( ) {
constructor ( ) { this.model = ‘Galaxy’;
Properties }
show() { return this.model +
}
“Price Rs 3000”;
} }
}
var nokia = new Mobile( );
Default Constructor
if you do not specify a constructor method a default constructor is used.

class Mobile { class Mobile {


constructor ( ) { show() { return this.model +
this.model = ‘Galaxy’; “Price Rs 3000”;
} }
show() { return this.model + }
“Price Rs 3000”; var nokia = new Mobile( );
}
}
var nokia = new Mobile( );
Parameterized Constructor
class Mobile {
constructor ( model_no ) {
this.model = model_no;
}
show() { return this.model + “Price Rs 3000”;
}
}
var nokia = new Mobile(‘Galaxy’);
Class Expression
Class expressions can be named or unnamed.
var Mobile = class { var Mobile = class Mobile2 {
constructor( ) { constructor( ) {
Properties Properties
} }
}; };
Class Hoisting
Class Declarations and Class Expression are not hoisted. You
first need to declare your class and then access it.
var nokia = new Mobile ( );
class Mobile {

class Mobile { }

} var nokia = new Mobile( ) ;


Inheritance

Parent Parent

Child Child

GrandChild
Class Inheritance
The extends keyword is used in class declarations or class
expressions to create a class which is a child of another class.
The extends keyword can be used to subclass custom classes as
well as built-in objects.
class Father {
}
Father

Son
class Son extends Father {
}
Class Inheritance
• Inherit Built-in Object
– Date
– String
– Array

class myDate extends Date {

}
Super
Super ( ) is used to initialize parent class constructor. If there is a constructor present in subclass, it
needs to first call super() before using "this". A constructor can use the super keyword to call the
constructor of a parent class.
Class Father {
constructor (money) {
this.Fmoney = money;
}
}
Class Son extends Father {
constructor (money){
super(money);
}
}
var s = new Son(10000);
Method Overriding
Same function name with different implementation.

Parent show ( ) {return “Super Class”; }

Child show
show(()){return
{return“Sub Class”;
“Super } }
Class”;
Static Method
The static keyword is used to define a static method for a class. Static methods
are called without creating object and cannot be called through a class instance
(object). Static methods are often used to create utility functions for an
application.
Ex:-
class Mobile {
constructor ( ) { }
static disp ( ) { return “Static Method”; }
}
Mobile.disp( ) ;

You might also like