When using Promise.all(), if any promise is rejected, the entire operation is considered _________.
- resolved
- pending
- rejected
- fulfilled
If any promise in the iterable passed to Promise.all() is rejected, the entire operation is considered rejected. All other promises are ignored, and the Promise.all() itself is rejected with the reason of the first rejected promise.
Arrow functions should not be used as ________ because they cannot be used with the new operator.
- Constructors
- Event Listeners
- Callbacks
- Promises
Arrow functions are not suitable for use as constructors since they lack a prototype property and cannot be used with the new operator. They are primarily designed for concise function expressions.
Q3: Consider an application that monitors the status of DOM elements. How would a WeakSet be beneficial in this context?
- WeakSet
- Set
- Both WeakSet and Set
- Neither WeakSet nor Set
In the context of monitoring DOM element status, a WeakSet is beneficial. This is because a WeakSet can hold references to DOM elements without preventing them from being garbage collected when they are removed from the DOM. As DOM elements are added and removed dynamically, a WeakSet ensures that the references do not cause memory leaks. If a DOM element is removed from the document, its reference in the WeakSet becomes automatically eligible for garbage collection. A Set, on the other hand, would keep references even if the DOM elements are removed, potentially leading to memory issues in the long run.
Mixins in ES6 are commonly used for adding _________ to classes without using inheritance.
- Properties
- Methods
- Behavior
- Variables
Mixins in ES6 are commonly used for adding behavior (methods) to classes without using traditional inheritance. This approach enhances code flexibility and avoids the potential downsides of deep class hierarchies.
What is the primary purpose of the spread operator in ES6?
- Spreading elements of an array or object
- Merging two arrays
- Concatenating strings
- Creating a new variable
The spread operator (...) is primarily used to spread elements of an array or object, allowing them to be expanded in places where multiple elements or variables are expected. This can be useful for passing array elements as individual arguments to a function or merging arrays.
How do default parameters affect the arguments object in a function?
- They override the arguments object
- They have no impact on the arguments object
- They are added to the arguments object
- They remove the arguments object
Default parameters in ES6 are added to the arguments object, making it a useful feature for accessing all passed arguments, even if they are not explicitly named in the function signature. This can be advantageous in certain scenarios where a dynamic number of arguments need to be handled.
Does a function that reads external files qualify as a pure function?
- Yes, as long as it only reads and does not modify
- No, any file reading operation is impure
- Only if it's a text file
- Only if it's a small file
No, reading external files is considered a side effect, and pure functions should not have side effects. Pure functions should only operate on their input parameters and not rely on external factors such as file systems or databases.
What is a use case for choosing WeakMap over Map?
- When the keys need to be weakly held and automatically removed
- When the keys have a strong reference and need to be retained
- When the map needs to be iterated in a predictable order
- When the map requires frequent updates
A use case for choosing WeakMap over Map is when the keys need to be weakly held and automatically removed when they are no longer referenced elsewhere in the code. This is beneficial for scenarios where precise memory management is crucial.
When applied to a string, the spread operator will split it into individual '__________'.
- Characters
- Substrings
- Arrays
- Elements
When applied to a string, the spread operator (...) will split it into individual characters, creating an array of characters. This is useful for tasks where you need to manipulate individual characters in a string or convert a string into an array of characters.
How does the yield keyword function in the context of iterators and iterables?
- Pauses the execution and returns a value to the iterator
- Ends the iterator immediately
- Skips the next iteration
- Continues the execution without interruption
In the context of iterators and iterables, the yield keyword is used to pause the execution of a generator function, returning a value to the iterator. When the generator is later called again, it resumes execution from where it was paused.