In JavaScript, classes are a special type of function that provides a convenient way to create objects with a similar structure. Classes were introduced in ECMAScript 6 (ES6) as a way to bring object-oriented programming concepts to JavaScript.
Here is a simple example of a class in JavaScript:
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.`);
}
}
const person = new Person('John Doe', 30);
person.sayHello(); // Hello, my name is John Doe and I am 30 years old.
In this example, the Person
class has a constructor that takes two arguments, name
and age
, and sets them as properties on the this
object. It also has a method sayHello
that logs a greeting to the console. To create a new instance of the Person
class, we use the new
operator and pass in the desired values for name
and age
.
-
In addition to constructor and methods, classes in JavaScript also support inheritance and polymorphism. This allows you to create complex and flexible object-oriented structures in your code.
-
Classes, methods, and inheritance are fundamental concepts in object-oriented programming, and JavaScript supports all of these concepts with its class syntax. Here's an example that demonstrates classes, methods, and inheritance in JavaScript:
class Shape { constructor(width, height) { this.width = width; this.height = height; } getArea() { return this.width * this.height; } } class Rectangle extends Shape { constructor(width, height) { super(width, height); } getPerimeter() { return 2 * (this.width + this.height); } } const rectangle = new Rectangle(10, 5); console.log(rectangle.getArea()); // 50 console.log(rectangle.getPerimeter()); // 30
In this example, we have a
Shape
class with awidth
andheight
property, as well as agetArea
method that calculates the area of the shape. We then have aRectangle
class that extends theShape
class. TheRectangle
class has its own constructor that simply calls thesuper
method to inherit the properties from theShape
class. TheRectangle
class also has agetPerimeter
method that calculates the perimeter of the rectangle. To use theRectangle
class, we create a new instance of it using thenew
operator and passing in the desiredwidth
andheight
values. We can then access thegetArea
andgetPerimeter
methods on therectangle
object to calculate the area and perimeter of the rectangle.
This example demonstrates how you can use classes, methods, and inheritance in JavaScript to create reusable and flexible object-oriented structures in your code.