1. What is OOP in JavaScript?
- Answer: Object-Oriented Programming (OOP) in JavaScript uses objects and classes to structure code.
class Animal {
speak() {
console.log("Animal speaks!");
}
}
const dog = new Animal();
dog.speak();
2. What is a Class in JavaScript?
- Answer: A class is a blueprint for creating objects with shared properties and methods.
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
}
3. What is an Object in JavaScript?
- Answer: An object is an instance of a class.
const myCar = new Car('Toyota', 'Corolla');
4. What is a Constructor in JavaScript?
- Answer: A constructor is a special method for initializing objects created by a class.
class User {
constructor(name) {
this.name = name;
}
}
const user = new User("John");
5. What is Inheritance in JavaScript?
- Answer: Inheritance allows a class to inherit properties and methods from another class.
class Vehicle {
start() {
console.log("Starting engine...");
}
}
class Car extends Vehicle {}
const car = new Car();
car.start();
6. What is Method Overriding in JavaScript?
- Answer: A child class can override a method from the parent class.
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
const dog = new Dog();
dog.speak();
7. What is super
in JavaScript?
- Answer:
super
is used to call the constructor of the parent class.
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
}
8. What is this
in JavaScript?
- Answer:
this
refers to the current object instance.
class Person {
constructor(name) {
this.name = name;
}
}
9. What is a Static Method in JavaScript?
- Answer: Static methods belong to the class, not instances.
class Calculator {
static add(a, b) {
return a + b;
}
}
console.log(Calculator.add(2, 3));
10. What is Encapsulation?
- Answer: Encapsulation restricts access to certain data and methods within an object.
class BankAccount {
#balance = 0; // private field
deposit(amount) {
this.#balance += amount;
}
}
11. What is Polymorphism?
- Answer: Polymorphism allows objects to be treated as instances of their parent class.
class Animal {
speak() {
console.log("Animal sound");
}
}
class Dog extends Animal {
speak() {
console.log("Bark");
}
}
12. What are Getters and Setters in JavaScript?
- Answer: Getters and Setters control access to object properties.
class User {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(name) {
this._name = name;
}
}
13. What is Prototypal Inheritance?
- Answer: JavaScript objects inherit properties from a prototype.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(`Hello, ${this.name}`);
};
14. What is Method Chaining?
- Answer: Method chaining allows multiple methods to be called in sequence on the same object.
class Calculator {
constructor(value = 0) {
this.value = value;
}
add(n) {
this.value += n;
return this;
}
subtract(n) {
this.value -= n;
return this;
}
}
const calc = new Calculator();
calc.add(5).subtract(2);
15. What is Closures in JavaScript?
- Answer: A closure is a function that retains access to its outer scope.
function counter() {
let count = 0;
return function() {
count++;
return count;
};
}
16. What is a Module Pattern in JavaScript?
- Answer: The Module Pattern encapsulates related variables and functions.
const Module = (function() {
let privateVar = "I am private";
return {
publicMethod: function() {
console.log(privateVar);
}
};
})();
17. What is instanceof
in JavaScript?
- Answer: Checks if an object is an instance of a specific class.
console.log(dog instanceof Dog); // true
18. What is the Factory Pattern?
- Answer: The Factory Pattern creates objects based on criteria without exposing the instantiation logic.
function createPerson(name, age) {
return {
name,
age,
greet() {
console.log(`Hi, I'm ${name}`);
}
};
}
19. What is the bind
method?
- Answer:
bind
sets thethis
context for a function.
const person = {
name: 'John',
greet() {
console.log(`Hello, ${this.name}`);
}
};
const greet = person.greet.bind(person);
greet();
20. What are Arrow Functions and this
?
- Answer: Arrow functions do not have their own
this
.
const obj = {
name: 'Arrow Example',
showName: () => console.log(this.name) // 'this' is not bound to 'obj'
};
21. What is call
in JavaScript?
- Answer:
call
invokes a function with a specifiedthis
context.
function introduce() {
console.log(`Hi, I'm ${this.name}`);
}
const person = { name: 'Alice' };
introduce.call(person);
22. What is apply
in JavaScript?
- Answer:
apply
is similar tocall
but takes an array of arguments.
introduce.apply(person, ['Hello']);
23. How does Prototypal Inheritance work?
- Answer: JavaScript uses prototypes for inheritance between objects.
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(this.name + ' makes a noise.');
};
const dog = new Animal('Dog');
dog.speak();
24. What is a Mixin?
- Answer: A mixin adds properties to an object without inheritance.
const canEat = {
eat() {
console.log("Eating...");
}
};
const person = Object.assign({}, canEat);
25. What are Symbols in JavaScript?
- Answer: Symbols are unique and immutable identifiers.
const sym = Symbol('unique');
const obj = { [sym]: 'hidden data' };
These questions cover essential JavaScript OOP concepts with explanations and examples to reinforce understanding.