91. What is throttling?
Throttling limits how frequently a function can execute within a given time interval.
Even if an event fires many times, the function is allowed to execute at most once during each interval.
Example:
function throttle(callback, delay) {
let lastTime = 0;
return function(...args) {
const now = Date.now();
if (now - lastTime >= delay) {
lastTime = now;
callback.apply(this, args);
}
};
}Common Uses:
โข Scroll events
โข Mouse movement
โข Window resizing
โข Continuous user interactions
Debounce vs Throttle:
Debounce โ Execute after activity stops
Throttle โ Execute at controlled intervals
92. What is memoization?
Memoization is an optimization technique where the result of a function is cached so that the same calculation doesn't have to be performed again.
Example:
function memoize(fn) {
const cache = new Map();
return function(n) {
if (cache.has(n)) {
return cache.get(n);
}
const result = fn(n);
cache.set(n, result);
return result;
};
}Example Usage:
function square(n) {
console.log("Calculating...");
return n * n;
}
const memoizedSquare = memoize(square);
console.log(memoizedSquare(5));
console.log(memoizedSquare(5));The first call performs the calculation.
The second call can use the cached result.
Benefits:
โ Faster repeated calculations
โ Avoids unnecessary work
Drawback:
โ Uses additional memory for cached results.
93. What is currying?
Currying transforms a function that takes multiple arguments into a sequence of functions that each take one argument.
Normal Function:
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2, 3));Curried Function:
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}Usage:
console.log(add(1)(2)(3));
Output:
6
Arrow Function Version:
const add = a => b => c => a + b + c;
Common Uses:
โข Functional programming
โข Creating reusable functions
โข Partial application
โข Configuration-based functions
94. What is prototype inheritance?
JavaScript uses objects as the foundation of its inheritance system.
Objects can access properties and methods through their prototype chain.
Example:
const animal = {
speak() {
console.log("Animal speaks");
}
};
const dog = Object.create(animal);
dog.speak();Although dog doesn't directly contain speak(), it can access the method through its prototype.
Prototype Chain:
dog
โ
animal
โ
Object.prototype
โ
null
JavaScript searches this chain when a property isn't found directly on the object.
95. What is prototypal inheritance?
Prototypal inheritance is JavaScript's mechanism for allowing one object to inherit behavior from another object through the prototype chain.
Example:
const person = {
greet() {
console.log("Hello");
}
};
const student = Object.create(person);
student.study = function() {
console.log("Studying");
};
student.greet();
student.study();Here:
student has its own study() method.
greet() comes from its prototype, person.
96. What are classes in JavaScript?
Classes provide a cleaner syntax for creating objects and implementing object-oriented programming patterns.
Classes were introduced in ES6.
Example:
