When a Promise is rejected, which method is typically used to handle the rejection?
- catch
- finally
- reject
- onReject
In the context of Promises, the catch method is commonly used to handle the rejection of a Promise. It allows you to specify a callback function that will be called if the Promise is rejected, providing a way to handle errors in asynchronous operations.
Consider a scenario where a module is exporting multiple functions, but only some are used. How does tree shaking impact this scenario?
- Only the used functions will be included in the final bundle, reducing its size.
- All exported functions will be included in the bundle.
- Tree shaking is not applicable to modules with multiple functions.
- The entire module will be excluded from the bundle.
Tree shaking analyzes the code to include only the necessary parts. In this scenario, only the functions that are used will be included, resulting in a smaller bundle size. The other options are incorrect, as tree shaking specifically targets unused code.
What is the result of referencing this inside a static method in an ES6 class?
- It refers to the instance of the class
- It refers to the class itself
- It causes a runtime error
- It depends on the context in which the static method is called
In a static method of an ES6 class, this refers to the class itself, not to an instance of the class. Static methods are called on the class, not on instances, so they don't have access to instance-specific properties or methods.
Can you use computed property names in ES6 object literals?
- Yes
- No
- Only in class declarations
- Only in arrow functions
Yes, in ES6 object literals, you can use computed property names, allowing you to dynamically set property names based on variables or expressions.
What is the primary advantage of using template literals over string concatenation?
- Enhanced Readability
- Better Performance
- Simplified Syntax
- Improved Browser Compatibility
Template literals provide enhanced readability due to their concise and expressive syntax. They allow embedding variables directly within the string using ${} syntax, making the code more readable and maintainable.
To handle multiple Promises concurrently and respond when the first one is fulfilled, use Promise._________.
- all()
- race()
- any()
- first()
The correct method is race(). It returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects, with the value or reason from that promise. This is useful for handling scenarios where you want to respond to the first promise that resolves.
How does a Set object handle NaN values?
- NaN is not allowed in a Set.
- It treats all NaN values as equal.
- Each NaN value is treated as a unique value in the Set.
- It throws an error when trying to add NaN.
Sets treat NaN values as equal. Therefore, adding multiple NaN values to a Set will result in having only one NaN value in the Set.
In a game, how would a static method be used to keep track of the total number of players created across all instances of the player class?
- Use an instance method and increment a player counter in each instance.
- Create a global variable to store the total number of players and update it in the player constructor.
- Implement a static method that increments a static counter in the player class.
- Utilize a separate counter class and delegate the counting task to it.
To keep track of the total number of players created across all instances in a game, a static method within the player class is suitable. This static method can maintain a static counter, ensuring it's shared among all instances and doesn't rely on the state of a specific player instance. Using a global variable may lead to scope-related issues and is less modular.
When developing an application with multiple dependent asynchronous API calls, would you choose Promises or callbacks? Explain why.
- Use Promises
- Use callbacks
- It depends on the specific requirements
- Combine Promises and callbacks
In modern JavaScript development, using Promises is a preferred choice for handling multiple dependent asynchronous API calls. Promises provide a cleaner and more readable syntax, making it easier to manage asynchronous code. Promises also allow for better error handling through the use of .catch() and can be easily chained, improving code maintainability.
Imagine a data processing module that requires several steps of data validation and transformation. How can functional composition and currying simplify this process?
- Enhancing Readability and Reducing Code Duplication
- Increasing Code Complexity and Maintenance
- Decreasing Modularity and Reusability
- Streamlining Error Handling and Debugging
Functional composition allows creating separate functions for each step of data processing, promoting modularity and reusability. Currying enhances this process by breaking down each step into smaller, composable functions. This results in a more readable, maintainable, and error-resistant data processing module. The combined use of functional composition and currying simplifies the entire data processing workflow and improves the overall efficiency of the system.