
A JavaScript class is a template for creating objects. Classes define a blueprint for an object, and can include properties (data) and methods (functions) that the object can use. Classes in JavaScript are defined using the class
keyword, which was introduced in ECMAScript 2015.
JavaScript classes use the concept of inheritance, which allows one class to inherit the properties and methods of another class. This is done using the extends
keyword. In addition to classes, JavaScript also supports the concept of object-oriented programming through the use of prototypes and factory functions.
Here is an example of a class in JavaScript:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
startEngine() {
console.log(`The ${this.make} ${this.model}'s engine has started.`);
}
}
let car = new Car("Tesla", "Model S");
car.startEngine();
In this example, Car
is the class name, and it has two properties, “make” and “model”, and one method, “startEngine”. The constructor
function is a special function that is called when a new object is created from the class. The new
keyword is used to create a new instance of the class and the properties of the class are passed as arguments to the constructor.
Here we created a new instance of the Car class, and passed “Tesla” as the make, and “Model S” as the model. The startEngine method is then called on the instance of the Car class, which logs “The Tesla Model S’s engine has started.”
How to Create a Class in JavaScript
In JavaScript, classes can be defined using the class
keyword. The basic syntax for defining a class is as follows:
class ClassName {
constructor(arg1, arg2, ...) {
// constructor code here
}
method1(args) {
// method code here
}
method2(args) {
// method code here
}
...
}
Here is an example of a class that defines a simple “Person” object:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
You can create an instance of this class using the new
keyword:
let person1 = new Person("John", 25);
person1.sayHello(); // will output "Hello, my name is John and I am 25 years old."
JavaScript classes also support inheritance, which allows one class to inherit the properties and methods of another class. This is done using the extends
keyword.
class Student extends Person {
constructor(name, age, major) {
super(name, age);
this.major = major;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old. I am studying ${this.major}.`);
}
}
let student1 = new Student("Jane", 22, "Computer Science");
student1.sayHello(); // will output "Hello, my name is Jane and I am 22 years old. I am studying Computer Science."
In this example, the Student
class inherits the properties and methods of the Person
class and adds a new property, “major”, and overrides the sayHello method. It’s worth noting that, since ECMAScript 2015, JavaScript also support the concept of a class through the use of prototypes and factory functions.