How does the lack of enumeration in WeakMap and WeakSet impact their use cases?
- Lack of enumeration in WeakMap and WeakSet prevents direct access to their elements.
- Enumeration is not essential in WeakMap and WeakSet, as they are designed for scenarios where enumeration is not needed.
- Lack of enumeration provides an added layer of security, making it difficult for external code to access the internal state of these structures.
- Enumeration is not relevant in the context of WeakMap and WeakSet, as their primary use is for memory management.
Lack of enumeration allows these structures to be more suitable for scenarios where privacy and encapsulation are critical.
How does tree shaking differ between named and default exports?
- Tree shaking is more effective with default exports
- Tree shaking is more effective with named exports
- Tree shaking treats both equally
- Tree shaking cannot be used with ES6 modules
Tree shaking is more effective with default exports because the bundler can directly eliminate the unused default export, whereas with named exports, the bundler may include unnecessary exports even if only one is used.
To define a function as a method in an object literal, ES6 syntax does not require the ________ keyword.
- function
- method
- this
- function
In ES6, when defining a method in an object literal, you can use the concise method syntax by omitting the "function" keyword, making the code more succinct and aligned with modern practices.
How does caching of modules work differently in ES6 Modules compared to CommonJS?
- ES6 Modules use a global cache for all modules
- ES6 Modules have no caching mechanism
- Each ES6 Module has its own cache
- CommonJS has no module caching
In ES6 Modules, each module maintains its own cache, preventing duplication of work and ensuring consistency across the application. CommonJS, on the other hand, uses a global cache for all modules.
Which of the following best describes the purpose of functional composition?
- Combining small, simple functions to build more complex ones
- Declaring variables globally for easy access
- Utilizing conditional statements for decision-making
- Executing functions in parallel for efficiency
The purpose of functional composition is to combine small, simple functions to build more complex and reusable functions. By composing functions together, developers can create a flow of data transformation, leading to cleaner and more maintainable code. This approach aligns with the principles of functional programming, emphasizing immutability and pure functions. A strong grasp of functional composition is essential for developers aiming to write expressive and efficient JavaScript code.
How does garbage collection in JavaScript affect WeakSet?
- WeakSet does not participate in garbage collection
- WeakSet triggers garbage collection on its own
- Garbage collection has no impact on WeakSet
- WeakSet entries are explicitly removed during garbage collection
Garbage collection has no impact on WeakSet. Unlike traditional sets, WeakSet does not prevent the referenced objects from being garbage-collected, making it useful in scenarios where you don't want to retain objects solely because they are part of a set.
In Promises, the _______ function is used to transition a promise into a resolved state, while _______ is used for rejection.
- resolve
- then
- reject
- catch
The correct option is 'resolve.' The resolve function transitions a promise into a resolved state, fulfilling the promise. On the other hand, the 'catch' function is used to handle rejection and is associated with the 'reject' state. The 'then' function is used for chaining and handles both fulfillment and rejection.
What is the default behavior of this at the top level in ES6 Modules, as opposed to CommonJS modules?
- Lexical scoping
- Dynamic scoping
- No scoping
- Global scoping
In ES6 Modules, the this keyword behaves according to lexical scoping, meaning it retains the value of this from the surrounding scope. This is different from CommonJS modules, where this refers to the exports object.
In what way does the Spread Operator interact with iterables?
- Spreads the iterable elements into a new array
- Converts the iterable into a string
- Ignores the iterable elements
- Creates a deep copy of the iterable
The Spread Operator (...) in ES6 is used to spread the elements of an iterable (e.g., an array) into a new array. It provides a concise way to concatenate arrays, create shallow copies, and pass array elements as function arguments.
In a for...of loop, using let allows each iteration to have its own _________ scope.
- Global
- Block
- Local
- Function
The let keyword creates a block-scoped variable, so each iteration in a for...of loop has its own block scope. This helps prevent unintended variable hoisting and makes the code more predictable.