A class is a template / blue print is used to create an object. In JavaScript class is a special kind of function. In JavaScript there are two ways to create class one is the class declaration and the second one is class expressions.
3. Introduction
A class is the blueprint / Template from which individual objects are
created.
In JavaScript class is special kind of function.
Example
Honda CBR, Bajaj Pulsar, TVS Apache all are comes under Byk class.
4. Class Definition
In JavaScript there are two components available to define a class
just like define a function.
Class Declaration
Class Expression
5. Class Definition – Class Declaration
One way to define a class.
To declare a class use class keyword with class name.
Syntax
class class_name {
// class body with constructor
}
6. Class Definition – Class Declaration(cont…)
Example
class Vehicle {
constructor(name, model){
this.name = name;
this.model = model;
}
}
7. Class Definition – Class Expressions
Another way to define a class.
Class expressions may be named or unnamed.
Syntax
var class_name = class { // unnamed
// class body with constructor
};
8. Class Definition – Class Expressions(cont…)
Syntax
var exp_name = class class_name { // named
// class body with constructor
};
9. Class Definition – Class Expressions(cont…)
Example
var Vehicle = class {
constructor(name, model){
this.name = name;
this.model = model;
}
}
10. Class Definition – Class Expressions(cont…)
Example
var Vehicle = class Vehicle{
constructor(name, model){
this.name = name;
this.model = model;
}
}
11. Static Methods
A method qualified with a qualifier static is called static method.
We can access static methods without creating object.
Syntax
static method_name([params]){
// method body
}
13. Inheritance
A class derived from another class.
Using inheritance we can get all the base class properties.
Syntax
class class_name extends base_class_name{
// class body
}