OOP in JavaScript (original) (raw)
What is a class in JavaScript?
- A built-in object for working with arrays
- A blueprint for creating objects
- A library for JavaScript functions
What is the purpose of the constructor method in a JavaScript class?
- Creates a new instance of the class
- Sets up properties for the class object
How do you create an instance of a class in JavaScript?
JavaScript `
class Person { constructor(name) { this.name = name; } }
`
- const p = new Person('Ajay');
- const p = Person('Ajay');
- const p = createInstance(Person, 'Ajay');
- const p = Object.create(Person);
What does the extends keyword do in JavaScript?
- Allows one class to inherit from another class
- Creates a new instance of a class
- Binds a method to a class
- Defines static methods in a class
What is encapsulation in JavaScript OOP?
- Using a constructor to initialize properties
- Inheriting properties from a parent class
- Combining multiple classes into one
- Restricting direct access to object properties and methods
What will the following code log?
JavaScript `
class Animal { makeSound() { console.log("Generic sound"); } } class Dog extends Animal { makeSound() { console.log("Bark"); } } const pet = new Dog(); pet.makeSound();
`
What is polymorphism in OOP?
- A way to define private properties
- Combining classes into a single object
- Using the same method name but with different implementations in child classes
- Sharing methods between unrelated objects
What are static methods in JavaScript classes?
- Methods only accessible to instances of the class
- Methods accessible only on the class itself
- Methods that are inherited by subclasses
- Methods that override the constructor
What will the following code log?
JavaScript `
class Vehicle { static start() { console.log("Starting vehicle..."); } } Vehicle.start();
`
Which of the following best describes inheritance in JavaScript?
- Creating a new class from a string
- Sharing properties and methods between objects of the same type
- Deriving a new class from an existing class to reuse code
- Encapsulating object properties
There are 10 questions to complete.
Take a part in the ongoing discussion