In object-oriented programming (OOP), classes are a fundamental concept that allows you to define a blueprint or template for creating objects. Here's how classes work in JavaScript:
In this example, we define a Dog class with an name instance variable and an age instance variable. We also define a constructor function that initializes these variables when a new Dog object is created. Finally, we define a bark method that logs the string "Woof!" to the console.
Instantiating objects:
code
// Create a new Dog object
let myDog = new Dog("Fido", 3);
// Access and manipulate instance variables
console.log(myDog.name); // Output: "Fido"
myDog.age = 7;
console.log(myDog.age); // Output: 7
In this example, we create a new Dog object using the new keyword, and then access and manipulate its instance variables using dot notation.
Inheritance:
code
// Define a subclass (Puppy) that inherits from Dog
class Puppy extends Dog {
// Add some additional instance variables and methods
favoriteToy: string;
playTime: number;
// constructor function overrides the parent class's constructor
constructor(name: string, age: number, favoriteToy: string) {
super(name, age);
this.favoriteToy = favoriteToy;
}
// add some additional methods
play() {
console.log("Playtime!");
}
}
In this example, we define a Puppy class that inherits from Dog. We add some additional instance variables and methods to the Puppy class, and modify the constructor function to override the parent class's constructor.
Polymorphism:
code
// Create instances of different classes
let myDog = new Dog("Fido", 3);
let myPuppy = new Puppy("Buddy", 4, "squeaky ball");
// Call methods on both objects
myDog.bark(); // Output: "Woof!"
myPuppy.play(); // Output: "Playtime!"
In this example, we create instances of both Dog and Puppy, and then call methods on both objects. Because Puppy inherits from Dog, we can call the bark method on a Puppy object just like we would on a Dog object. This is an example of polymorphism, where objects of different classes can be treated as if they were of the same class.
Encapsulation and abstraction:
code
// Define a Dog class with a private `_name` property
class Dog {
private _name: string;
constructor(name: string) {
this._name = name;
}
// Access the private property through a getter function
get name() {
return this._name;
}
}
// Create a new Dog object
let myDog = new Dog("Fido");
// Access the private property directly
console.log(myDog._name); // Output: "Fido"
In this example, we define a Dog class with a private _name property that can only be accessed through a getter function. This is an example of encapsulation, where the internal state of an object is hidden from the outside world and can only be accessed through a controlled interface.
Interfaces:
code
// Define an interface for a Pet class
interface Pet {
name: string;
age: number;
speak(): void;
}
// Define a Dog class that implements the Pet interface
class Dog implements Pet {
// Implement the interface methods
name: string;
age: number;
speak() {
console.