class Cat {
constructor(name, age, breed, owner, location) {
this.name = name;
this.age = age;
this.breed = breed;
this.owner = owner;
this.location = location;
}
// Method to meow
meow() {
console.log(`${this.name} says: Meow!`);
}
// Method to fetch and display a random cat image using an API
async fetchRandomCatImage() {
try {
const response = await fetch('https://api.thecatapi.com/v1/images/search');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const imageUrl = data[0].url;
console.log(`${this.name}'s random cat image: ${imageUrl}`);
} catch (error) {
console.error('Error:', error);
}
}
}
// Usage example:
const myCat = new Cat("MaoMao", 11, "Tuxedo", "Lindsay", "Bedroom");
// Accessing cat properties
console.log(`Name: ${myCat.name}`);
console.log(`Age: ${myCat.age}`);
console.log(`Breed: ${myCat.breed}`);
console.log(`Owner: ${myCat.owner}`);
console.log(`Location: ${myCat.location}`);
// Calling a method
myCat.meow();
// Fetch and display a random cat image
myCat.fetchRandomCatImage();
Cat Classes • 2 min read
Description
This is a demonstration of the JavaScript outputs. This class is called Cat. You can add cats into the class. You can also use the API fetch to retrieve cat images.