What is the significance of weak references in WeakMap and WeakSet with regards to memory management?
- Weak references in WeakMap and WeakSet allow for automatic garbage collection, preventing memory leaks.
- Weak references are irrelevant in the context of WeakMap and WeakSet, as they don't impact memory management.
- Weak references provide a way to create circular references, enhancing memory efficiency.
- Weak references ensure that the referenced objects are not eligible for garbage collection, preserving memory.
Weak references in WeakMap and WeakSet enable automatic memory management by allowing the garbage collector to reclaim objects that are no longer reachable.
What is the initial state of a JavaScript Promise when it is created?
- Pending
- Resolved
- Rejected
- Settled
When a Promise is created, it starts in the "Pending" state. This means that the Promise is neither fulfilled nor rejected. It's essentially in a pending state until the asynchronous operation it represents is completed.
In an async function, what happens to unhandled exceptions?
- They are automatically caught by the global error handler
- They cause the program to crash immediately
- They propagate to the nearest catch block
- They result in an unhandled promise rejection
Unhandled exceptions in an async function result in unhandled promise rejections. This can lead to unexpected behavior or crashes if not properly addressed. It's crucial to handle errors appropriately in async functions to ensure robust and error-resistant code.
In a project where a class needs to incorporate event-handling, logging, and validation behaviors, how would mixins and composition be used?
- By using inheritance and overriding methods in the class.
- By applying mixins through multiple inheritance.
- By using composition and creating separate classes for each behavior, then combining them.
- By using mixins and composing them into the class.
Mixins in ES6 can be applied by creating separate modules for each behavior (event-handling, logging, validation) and then composing these mixins into the class. This promotes code reusability and maintainability.
The __________ is responsible for executing the code, collecting and processing events, and executing queued sub-tasks.
- Callback Queue
- Event Loop
- Promise
- Closure
The Event Loop is a crucial part of JavaScript's concurrency model. It continuously checks the Call Stack and Callback Queue, ensuring the non-blocking execution of code.
When the call stack is busy, new requests get queued in the __________ for their turn to be processed.
- Callback Queue
- Microtask Queue
- Event Loop
- Task Queue
The correct option is Task Queue. When the call stack is occupied, new tasks are placed in the task queue to await their turn. The Event Loop manages the process of moving tasks from the task queue to the call stack. Understanding this mechanism is crucial for handling asynchronous operations effectively.
To create a pipeline of operations, functions can be composed using higher-order functions, known as __________.
- Callbacks
- Promises
- Closures
- Combinators
In functional programming, functions like compose or pipe are used as combinators to create a pipeline of operations by composing other functions. These are higher-order functions that take functions as arguments and return a new function that represents the composition of those functions.
__________ methods in ES6 classes are methods that are bound to the class itself, not to instances of the class.
- Static
- Instance
- Prototype
- Class
Static methods in ES6 classes are defined using the static keyword and are bound to the class itself rather than to instances of the class. These methods are associated with the class constructor and are called on the class, not on instances. Static methods are often used for utility functions that are related to the class but don't depend on specific instance data.
To re-export all named exports from a module, use export * from _________.
- export * from 'moduleName';
- export all from 'moduleName';
- export {all} from 'moduleName';
- export named from 'moduleName';
To re-export all named exports from a module in ES6, you use the export * from 'moduleName'; syntax. This allows you to export all the named exports from one module to another without listing them individually.
Q1: Imagine you are creating a caching mechanism for a web application. Would you choose a WeakMap or a Map for storing cached data and why?
- WeakMap
- Map
- Both WeakMap and Map
- Neither WeakMap nor Map
In a caching mechanism, using a WeakMap is preferred over a Map. This is because a WeakMap allows the keys to be garbage collected if there are no other references to them, which is beneficial for caching scenarios where you want the cached data to be automatically cleared when the corresponding objects are no longer in use. A Map, on the other hand, would retain the keys even if they are not needed anymore, leading to potential memory leaks in a caching context.