Classes in JavaScript
In JavaScript, a class is a type of function that is used to create objects. It is similar to a blueprint for an object. You can define the properties and methods that an object should have, and then create multiple instances of the class (objects) that all have the same properties and methods.
Here is an example of a class in JavaScript:
class Animal {
constructor(name, type) {
this.name = name;
this.type = type;
}
makeNoise() {
console.log("Some noise");
}
}
You can then create an instance of the class (an object) like this:
const dog = new Animal("Fido", "dog");
You can access the properties and methods of the object like this:
console.log(dog.name); // "Fido"
console.log(dog.type); // "dog"
dog.makeNoise();
// "Some noise"
No comments:
Post a Comment