JavaScript - Advanced Flashcards
call()
The call() method invokes a function with a specified this value and arguments provided individually.
For Example:
function greet(name) {
console.log(Hello, ${name}!
);
}
greet.call(null, ‘John’);
apply()
The apply() method is similar to call(), but it accepts arguments as an array or an array-like object.
For Example:
function greet(name) {
console.log(Hello, ${name}!
);
}
greet.apply(null, [‘John’]);
bind()
The bind() method creates a new function with a specified this value and partially applies arguments.
For Example:
function greet(name) {
console.log(Hello, ${name}!
);
}
const greetJohn = greet.bind(null, ‘John’);
greetJohn();
Lexical Scope
Lexical scoping means that the scope of a variable is determined by its location within the source code.
Variables are accessible within their containing functions or blocks.
Dynamic Scope
Dynamic scoping, on the other hand, determines the scope of a variable based on the flow of the program during runtime.
The scope depends on the call stack.
Explain the concept of memoization in JavaScript
Memoization is a technique to cache the results of expensive function calls and return the cached result when the same inputs occur again.
For Example:
function memoize(fn) {
const cache = {};
return function(…args) {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key];
}
const result = fn.apply(this, args); cache[key] = result; return result; }; }
const fibonacci = memoize(function(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
});
console.log(fibonacci(5)); // Returns 5
console.log(fibonacci(10)); // Returns 55
How does Memoization improve performance?
It improves performance by avoiding unnecessary computations.
What is the process that JavaScript follows for Memory Management and Garbage Collection?
Allocation
Use
Deallocation
Garbage Collection
Garbage Collection Allocation
When objects are created, memory is allocated to store them.
Garbage Collection Use
Objects are used and referenced by variables or other objects.
Garbage Collection Deallocation
When objects are no longer needed, memory is deallocated to free up resources.
Garbage Collection Step
JavaScript’s garbage collector periodically identifies and removes objects that are no longer reachable, i.e., not referenced by any variables or objects.
Different techniques for optimizing JavaScript code
Minification
Bundling
Caching
Debouncing
Memoization
Minification
Removing unnecessary characters (whitespace, comments) to reduce file size.
Bundling
Combining multiple JavaScript files into a single file to reduce network requests.
Explain the concept of function composition in JavaScript
Function composition is the process of combining multiple functions to create a new function that performs multiple operations.
For Example:
const add = (a, b) => a + b;
const multiply = (a, b) => a * b;
const pipe = (…fns) => (x) => fns.reduce((y, fn) => fn(y), x);
const addAndMultiply = pipe(add, multiply);
console.log(addAndMultiply(2, 3)); // Returns 10 (2 + 3 = 5, 5 * 2 = 10)