What happens if a class in ES6 does not have a constructor?
- It throws an error
- It automatically creates an empty constructor
- It inherits the constructor from the parent class
- It is not possible to have a class without a constructor
In ES6, if a class does not have a constructor, a default constructor is automatically created. This default constructor is empty and does nothing. This behavior helps in cases where a class does not require any specific initialization logic in its constructor.
If you are creating a function to navigate through a tree-like data structure, how would recursion aid in this process?
- Recursively call the function for each child node, allowing traversal through the entire tree.
- Utilize a for loop to iterate over each level of the tree structure.
- Apply the Array.prototype.map method to map through the tree nodes.
- Use the Array.prototype.filter method to filter out unwanted nodes in the tree.
Recursion is instrumental in navigating a tree-like data structure by allowing the function to traverse through each level of the tree effortlessly. The recursive call on each child node ensures a thorough exploration of the entire structure. Other options lack the inherent nature of recursion for tree traversal.
How does error handling differ in async/await compared to traditional promise-based approaches?
- Both handle errors in the same way
- Async/await uses try...catch for error handling
- Promises use try...catch for error handling
- Async/await relies on callback functions
In async/await, errors are handled using the familiar try...catch block, making it easier to write and read asynchronous code. This improves the debugging experience compared to chaining .then() and .catch() in promise chains.
What happens when you add duplicate elements to a Set?
- Ignored, Overwritten, Error, Concatenated
- Overwritten, Ignored, Concatenated, Error
- Concatenated, Overwritten, Ignored, Error
- Ignored, Concatenated, Error, Overwritten
When you add duplicate elements to a Set in JavaScript, they are ignored. Unlike Arrays or Objects, Sets only allow unique values, ensuring that the Set contains only distinct elements. Adding a duplicate element does not result in an error, and the Set remains unchanged.
What types of values can be added to a WeakSet?
- Only objects can be added.
- Any data type, including primitives, can be added.
- Only functions can be added.
- Only arrays can be added.
WeakSets exclusively allow objects, emphasizing their use for storing unique object references and aiding in scenarios like memory management.
Can a pure function modify a global variable?
- Yes
- No
- It depends
- Only if it's a constant variable
No, a pure function should not modify global variables or any external state. It should only depend on its input parameters to produce the output and should not have side effects. Modifying global variables violates the principles of pure functions.
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.