In a web application, how can higher-order functions be used to enhance event handling?
- reduce
- map
- forEach
- filter
The correct option is forEach. forEach is commonly used for iterating over elements in an array, making it suitable for enhancing event handling in a web application where actions need to be performed on each element.
Consider a scenario where you need to add common functionalities to several unrelated classes in ES6. How would mixins and composition address this requirement?
- By using inheritance to create a base class with common functionalities.
- By creating separate mixins for each functionality and applying them to the unrelated classes.
- By directly adding methods to each unrelated class.
- By creating a utility class that encapsulates the common functionalities and using composition to add it to each class.
Mixins and composition can address this requirement by creating separate mixins for each functionality and applying them to the unrelated classes. This promotes code reuse without the need for inheritance.
If you're building a library with a primary functionality and several auxiliary functions, how would you organize your exports?
- Using named exports for each auxiliary function
- Using a default export for the primary functionality and named exports for auxiliary functions
- Using a default export for each function, combining primary and auxiliary functionalities
- Using a single named export for the entire library
When building a library, using a default export for the main functionality and named exports for auxiliary functions is a common practice. This way, consumers can import only the specific functionalities they need, optimizing bundle size and avoiding unnecessary code.
In a WeakMap, keys must be of type _________, and they are not enumerable.
- Object
- String
- Symbol
- Number
In a WeakMap, keys must be of type Symbol, and they are not enumerable. Symbols are unique and help in creating private properties or methods. Using other types as keys will result in unexpected behavior.
How would you use mixins in ES6 to enhance classes in a library, without modifying the original source code?
- By directly modifying the source code of the original class.
- By creating a new class that extends the original class and includes the desired mixins.
- By using decorators to dynamically apply mixins to the original class.
- By creating a higher-order function that adds mixins to the original class prototype.
To enhance classes in a library without modifying the source code, you can create a new class that extends the original class and includes the desired mixins. This follows the concept of class extension and composition.
In a scenario where a function needs to access the this keyword of a containing object, should an arrow function be used?
- Yes, always
- No, never
- It depends
- Only in classes
Arrow functions should not be used in this scenario because they do not bind their own "this" and instead inherit it from the enclosing scope. This can lead to unintended behavior when trying to access the "this" keyword of a containing object.
In a scenario where a function receives a complex object as an argument, how can destructuring assignment enhance code readability and maintenance?
- It has no effect on code readability
- It makes code more complex
- It simplifies the extraction of values from the object
- It can only be used with primitive data types
When a function receives a complex object as an argument, using destructuring assignment can significantly enhance code readability and maintenance. By specifying the necessary properties directly in the function parameter, developers can easily understand which values the function expects. This not only simplifies the code but also reduces the need for repeated object property access, making the code more efficient and easier to maintain.
Consider a scenario where you are handling multiple independent API calls. How would the approach differ using Promises compared to callbacks?
- Promises allow for better parallel execution
- Callbacks are more suitable for independent calls
- Promises are slower for independent calls
- Callbacks lead to callback hell
When dealing with multiple independent API calls, Promises offer a significant advantage by allowing for better parallel execution. Unlike callbacks, which may result in nested structures (callback hell), Promises provide a more elegant solution through chaining, resulting in cleaner and more maintainable code.
Consider a function designed to format a date. How would you use default parameters to provide flexibility in the format?
- Utilize default parameters to allow users to specify the format, such as formatDate(date, format = "YYYY-MM-DD").
- Use default parameters for individual components like day, month, and year, enabling customization while maintaining a default format.
- Allow a default format but also accept an optional format parameter, giving users the ability to override the default if needed.
- Use default parameters to set a default format but allow users to pass a custom formatting function, giving maximum flexibility.
Default parameters in JavaScript allow you to assign default values to function parameters. In the context of formatting a date, default parameters can be used to provide a default format, and users can override this by passing their desired format as an argument. This provides a balance between having a predefined format and allowing customization based on user needs.
What is the outcome of trying to mutate the properties of an object declared with const?
- Mutation is allowed
- Mutation is not allowed
- Mutation is allowed, but with restrictions
- Mutation is allowed only for primitive properties
When an object is declared with const, its reference cannot be reassigned, but the properties can be mutated. It's important to note that const protects the reference, not the content. So, attempting to reassign the entire object will result in an error, but modifying its properties is permissible.