[go: up one dir, main page]

0% found this document useful (0 votes)
33 views13 pages

ES 6 Class and Inheritance

JavaScript classes are introduced in ES6 and classes are special functions. There are two ways to define a class using the class keyword: class declaration and class expression. The constructor method is used to initialize an object and there can only be one constructor per class. If no constructor is provided, a default constructor is used. The extends keyword is used for class inheritance and super() must be called in the child constructor to initialize the parent constructor. Methods can be overridden in child classes. Static methods are called on the class without an instance.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

ES 6 Class and Inheritance

JavaScript classes are introduced in ES6 and classes are special functions. There are two ways to define a class using the class keyword: class declaration and class expression. The constructor method is used to initialize an object and there can only be one constructor per class. If no constructor is provided, a default constructor is used. The extends keyword is used for class inheritance and super() must be called in the child constructor to initialize the parent constructor. Methods can be overridden in child classes. Static methods are called on the class without an instance.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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