15.
What is a class in ES6?
A JavaScript function that is used to perform a specific task.
A data structure used to store multiple values of different types.
A way to define a set of related functions in JavaScript.
A blueprint for creating objects with predefined properties and methods.
Correct: D
In ES6, a class is a syntactic sugar for creating objects with predefined properties and methods. It provides a way to define and create objects using the same syntax and structure, making the code more readable and maintainable. Classes can have constructors, methods, and inheritance, allowing for creating complex and organized object-oriented code.
// Define a class called "Person"
class Person {
// Constructor method to initialize object properties
constructor(name, age) {
this.name = name;
this.age = age;
}
// Method to greet
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
// Create an instance of the "Person" class
const person1 = new Person("Alice", 30);
// Call the "greet" method of the object
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old.