This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
8.1 JS Classes
ECMAScript 2015, also known as ES6, introduced JavaScript Classes.
JavaScript Classes are templates for JavaScript Objects.
JavaScript Class Syntax
Use the keyword class to create a class.
Always add a method named constructor() :
Syntax
class ClassName {
constructor() { ... }
}
Example
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
The example above creates a class named "Car".
The class has two initial properties: "name" and "year".
A JavaScript class is not an object.
It is a template for JavaScript objects.
Using a Class
When you have a class, you can use the class to create objects:
let myCar1 = new Car("Ford", 2014);
let myCar2 = new Car("Audi", 2019);
The example above uses the Car class to create two Car objects.
The constructor method is called automatically when a new object is created.
The Constructor Method
The constructor method is a special method:
It has to have the exact name "constructor"
It is executed automatically when a new object is created
It is used to initialize object properties
If you do not define a constructor method, JavaScript will add an empty constructor method.
Class Methods
Class methods are created with the same syntax as object methods.
Use the keyword class to create a class.
Always add a constructor() method.
Then add any number of methods.
Syntax
class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
method_3() { ... }
}
}
Create a Class method named "age", that returns the Car age:
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
let date = new Date();
return date.getFullYear() - this.year;
}
}
let myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
You can send parameters to Class methods:
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age(x) {
return x - this.year;
}
}
let date = new Date();
let year = date.getFullYear();
let myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML=
"My car is " + myCar.age(year) + " years old.";
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Classes
JS Class Methods part 1
JS Class Methods part 2
JS Modules
Create Object from JSON String
JS Debuggers The console.log() Method
JS Debuggers The debugger Keyword
JS Style Guide - Line Length
JS Literal Constructors
JS Variables
JS Variables - Automatic Type Conversions
JS Variables - NaN
JS Comparisons
JS Functions - Parameter Defaults
End Your Switches with Defaults
JS String Objects cannot be safely compared
JS Objects cannot be compared
JS Common JavaScript Mistakes
JS Breaking a JavaScript Statement part 1
JS Breaking a JavaScript Statement part 2
JS Breaking a JavaScript Statement part 3
JS Breaking a Return Statement part a
JS Breaking a Return Statement part b
JS Breaking a Return Statement part c
JS Breaking a Return Statement part d
JS Breaking a Return Statement part 3f
JS Accessing Arrays with Named Indexes part 1
JS Accessing Arrays with Named Indexes part 2
JS Objects - Undefined is Not Null
JS Performance - Reduce DOM Access