What is a closure in JavaScript?
A closure is when a function remembers and can access variables from its outer scope even after that outer function has finished executing.
What is memoization used for?
Memoization is used to speed up functions by caching the results of expensive function calls and returning the cached result when the same input occurs again.
How does memoization typically use closures?
Memoization uses closures to maintain a private cache object that persists across calls to the memoized function.
What is Object-Oriented Programming (OOP) in JavaScript?
A programming paradigm based on objects and classes to model real-world things using properties and methods.
What are the four main principles of OOP?
Encapsulation
What is encapsulation?
Hiding the internal details of an object and exposing only what is necessary through public methods.
What is abstraction?
Hiding complex implementation details and showing only the necessary features of an object.
What is inheritance in JavaScript?
A mechanism where one object can acquire properties and methods from another object.
What is polymorphism?
The ability of different objects to be accessed through the same interface
How do you create an object in JavaScript?
Using object literals {}
What is a class in JavaScript?
A blueprint for creating objects with shared properties and methods introduced in ES6.
How do you define a class in JavaScript?
Using the ‘class’ keyword, followed by the class name and a constructor method.
What is a constructor method?
A special method used for initializing new objects created from a class.
How do you create an instance of a class?
Using the ‘new’ keyword followed by the class name.
What is the ‘this’ keyword in JavaScript?
Refers to the current object instance within a method.
What is method overriding?
Providing a new implementation of a method inherited from a parent class.
What is a prototype in JavaScript?
An object from which other objects inherit properties and methods.
What is the prototype chain?
A mechanism where objects inherit from other objects
How does JavaScript handle inheritance?
Through prototypes and the prototype chain.
What is the difference between class-based and prototype-based OOP?
Class-based uses classes and instances; prototype-based uses objects and their prototypes directly.
Can you modify a prototype?
Yes
What is the benefit of using prototypes?
It allows sharing methods across all instances
How can you achieve encapsulation in JavaScript?
Using closures or the new private fields (#) syntax inside classes.
What are private fields in JavaScript classes?
Properties prefixed with # that cannot be accessed outside the class.