1. What is a class in JavaScript?
Answer:
Explanation:
In JavaScript, a class is a blueprint for creating objects with pre-defined properties and methods.
2. How do you define a class in JavaScript?
Answer:
Explanation:
A class in JavaScript is defined using the class keyword followed by the class name and a set of curly braces containing its body.
3. What is a constructor in a JavaScript class?
Answer:
Explanation:
The constructor is a special method within a class that is called when a new instance of the class is created, used for initializing properties of the object.
4. How do you create an instance of a class in JavaScript?
Answer:
Explanation:
An instance of a class is created using the new keyword followed by the class name and parentheses.
5. What is inheritance in the context of JavaScript classes?
Answer:
Explanation:
Inheritance in JavaScript classes is a mechanism where one class (child class) can inherit properties and methods from another class (parent class).
6. How do you specify a class as a child of another class in JavaScript?
Answer:
Explanation:
The extends keyword is used in class declarations to create a class as a child of another class.
7. What is a method in a JavaScript class?
Answer:
Explanation:
A method in a JavaScript class is a function defined inside the class that describes the behaviors of the class's instances.
8. Can classes in JavaScript have private properties or methods?
Answer:
Explanation:
JavaScript classes can have private properties and methods, which are defined using the # symbol in front of their names. These are only accessible within the class itself.
9. What is the use of the static keyword in a JavaScript class?
Answer:
Explanation:
The static keyword is used to define static methods or properties in a class. Static methods/properties are called without instantiating the class and cannot be called through a class instance.
10. How do you call a parent class's method from within a child class's method?
Answer:
Explanation:
The super keyword is used within a child class to call methods or the constructor of the parent class.
11. What is encapsulation in the context of JavaScript classes?
Answer:
Explanation:
Encapsulation in JavaScript classes refers to the bundling of data (properties) and the methods that operate on that data into a single unit or class.
12. How can you override a method in a child class in JavaScript?
Answer:
Explanation:
Overriding a method in a child class is done by redeclaring the method in the child class. This new method will replace the inherited method from the parent class.
13. What is polymorphism in the context of JavaScript classes?
Answer:
Explanation:
Polymorphism in object-oriented programming, including JavaScript classes, is the ability of different classes to provide different implementations of methods that are called through the same name.
14. Can you declare variables inside a class in JavaScript without a method?
Answer:
Explanation:
In JavaScript classes, variables cannot be declared directly inside a class but must be declared within methods or as part of the class's properties.
15. What is the result of instantiating an abstract class in JavaScript?
Answer:
Explanation:
JavaScript does not have native support for abstract classes. Attempting to instantiate a class that is intended to be abstract will not throw an error but will not enforce abstract behavior.