JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Classes

JavaScript (JS) is a popular programming language used for creating interactive web pages. It is a versatile language that can be used for both front-end and back-end development. One of the key features of JS is its ability to create classes, which are a fundamental part of object-oriented programming (OOP).

JS classes provide a way to define a blueprint for creating objects. They allow developers to create objects with similar properties and methods, making it easier to manage and organize code. In this tutorial, we will explore the basics of JS classes and how they can be used in web development.

Brief Explanation of JS Classes

JS classes are a way to define a template for creating objects. They are similar to functions, but with some key differences. Classes can have properties and methods, just like objects, but they are not executed until an instance of the class is created.

Classes are defined using the class keyword, followed by the name of the class. Properties and methods are defined within the class using the constructor method and other methods, respectively.

Here is an example of a simple JS class:

<script>
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 john = new Person('John', 30);
john.sayHello();
</script>

In this example, we define a class called Person with a constructor method that takes two parameters, name and age. We then define a method called sayHello that logs a message to the console using the name and age properties.

We then create an instance of the Person class called john and call the sayHello method on it. This will log the message "Hello, my name is John and I am 30 years old." to the console.

JS classes can also inherit properties and methods from other classes using the extends keyword. This allows developers to create more complex class hierarchies and reuse code.

Code Examples

Here are some more examples of JS classes:

Animal Class

<script>
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks.`);
  }
}

const dog = new Dog('Rufus');
dog.speak();
</script>

In this example, we define an Animal class with a constructor method that takes a name parameter and a speak method that logs a message to the console. We then define a Dog class that extends the Animal class and overrides the speak method to log a different message.

We create an instance of the Dog class called dog and call the speak method on it. This will log the message "Rufus barks." to the console.

Rectangle Class

<script>
class Rectangle {
  constructor(width, height) {
    this.width = width;
    this.height = height;
  }

  get area() {
    return this.width * this.height;
  }

  set area(value) {
    this.width = value / this.height;
  }
}

const rect = new Rectangle(10, 20);
console.log(rect.area);

rect.area = 200;
console.log(rect.width);
</script>

In this example, we define a Rectangle class with a constructor method that takes width and height parameters. We then define a get accessor for the area property that calculates the area of the rectangle and a set accessor that sets the width property based on the desired area.

We create an instance of the Rectangle class called rect and log the area property to the console. We then set the area property to 200 and log the width property to the console. This will output "40" to the console, as the width of the rectangle has been adjusted to maintain the desired area.

Conclusion

JS classes are a powerful feature of the language that allow developers to create reusable code and manage complex object hierarchies. They are an essential part of OOP and are widely used in web development. By understanding the basics of JS classes, developers can create more efficient and maintainable code.

References

Activity