When designing a function to log user activity, what considerations should be made to maintain purity and manage side effects?
- Store logs in a global array
- Use asynchronous logging for better performance
- Log only user actions without any additional data
- Separate the logging logic from the main function
To maintain purity and manage side effects, the logging logic should be separated from the main function. This separation helps in isolating the side effect (logging) and makes the main function pure. Storing logs in a global array or mixing logging with the main function can lead to impurity.
When designing a class for a UI component, how would you define a method that shouldn't be accessible outside of the class?
- Private Methods
- Protected Methods
- Public Methods
- Static Methods
In ES6, to create a method that is not accessible outside of the class, you can use the # symbol before the method name, making it a private method. Private methods are encapsulated within the class, ensuring they are not accessible externally. This helps in controlling access to specific functionalities and maintaining the integrity of the class.
How do iterators use the next() method, and what does it return?
- Iterators use next() to move to the next element and return an object with value and done properties
- Iterators use next() to check if an element exists and return a boolean value
- Iterators use next() to retrieve the previous element
- Iterators use next() to skip to the end of the iteration
The next() method is called on an iterator to move to the next element in the iteration. It returns an object with two properties: value, which contains the current element, and done, a boolean indicating whether the end of the iteration has been reached. This allows for controlled and step-by-step iteration over a sequence of values.
When using named exports, the import names must _________ the exported names unless using the as keyword.
- exactly match
- differ from
- be optional
- be prefixed with 'import'
When using named exports, the import names must exactly match the exported names unless using the 'as' keyword.
If you have a utility class for a shopping application, how would you implement a method that calculates the total discount for all users?
- Implement a static method within the utility class to calculate discounts.
- Use an instance method and iterate through each user, calculating and accumulating discounts.
- Create a separate discount calculation class and delegate the task to it.
- Utilize a global variable to store the total discount and update it in each user instance.
In a scenario like a shopping application, it's beneficial to use a static method within the utility class to calculate the total discount. This way, the method is associated with the class itself, not an instance, and can be easily accessed without creating unnecessary instances for the calculation. Static methods are appropriate for operations that don't depend on the state of a specific instance.
Can you enumerate the keys of a WeakMap?
- Yes, you can use the getKeys method.
- No, WeakMaps do not have a method to retrieve all keys.
- Yes, you can use a loop to iterate over the keys.
- No, keys in WeakMaps cannot be enumerated.
WeakMaps prioritize privacy by not providing a direct way to enumerate keys, contributing to their use in scenarios where key visibility is a concern.
In a Promise chain, where should you place a .catch() method for centralized error handling?
- At the beginning of the Promise chain.
- At the end of the Promise chain.
- It can be placed anywhere within the Promise chain.
- Immediately after the last then() block in the Promise chain.
To achieve centralized error handling in a Promise chain, the .catch() method should be placed immediately after the last then() block. This ensures that any error thrown at any stage of the Promise chain is caught centrally, providing a clean and organized way to handle errors in asynchronous operations.
What is the primary purpose of a constructor in a JavaScript class?
- Initializing class properties
- Defining class methods
- Controlling class inheritance
- Managing private class members
The primary purpose of a constructor is to initialize class properties, setting up the initial state of the object. Constructors are called when an object is created, ensuring that the object starts with the desired values.
Consider a scenario where you need to sequentially process a list of URLs to fetch data. How would you structure your async/await function to achieve this?
- Use Promise.all for parallel processing
- Utilize a loop with await inside to process URLs sequentially
- Employ async.each for sequential URL processing
- Mix Promise.all and Promise.race for optimized sequential processing
To sequentially process a list of URLs, structure your async/await function with a loop that utilizes await inside. This ensures each URL is processed one after the other, maintaining order and avoiding parallel execution.
How does immutability relate to pure functions in JavaScript?
- Enhances predictability
- Minimizes side effects
- Allows for asynchronous operations
- Supports object mutation
In JavaScript, immutability is closely tied to the concept of pure functions. Pure functions do not modify external state, which promotes predictability and makes it easier to reason about code. Immutability ensures that once a data structure is created, it cannot be changed, aligning with the principles of pure functions.
How is a class method defined in ES6?
- function methodName() { }
- method methodName() { }
- methodName() { }
- class methodName() { }
In ES6, a class method is defined without the function keyword. It is declared directly within the class body using the syntax methodName() { }. This syntax is concise and aligns with modern JavaScript practices.
How does the filter method determine which elements to include in the returned array?
- Based on whether elements pass a specified condition provided by a callback function.
- Returns elements at even indices in the array.
- Sorts the array in ascending order and returns the elements that meet a certain criterion.
- Includes all elements except the ones specified in the callback function.
The filter method creates a new array with all elements that pass a test implemented by the provided function. The callback function determines the condition for inclusion, and only elements for which the function returns true are included in the new array. This is commonly used for selecting a subset of elements based on a specific condition.