Consider a library that needs to add unique identifiers to objects without risk of property clashes. How would Symbols be utilized in this case?
- Using string identifiers
- Using Symbols as unique identifiers
- Using numbers as identifiers
- Using object literals with unique keys
The correct option is using Symbols as unique identifiers. Symbols provide a way to create unique keys for object properties, ensuring that the identifiers added by the library are distinct and not prone to clashes. Using string identifiers or numbers may lead to conflicts, especially in a shared library context. Object literals with unique keys can be used, but Symbols offer a more direct and elegant solution.
In a custom iterable, the next() method should return an object with two properties: value and ________.
- done
- index
- result
- previous
In a custom iterable, the next() method should return an object with two properties: value, representing the current value in the iteration, and done, a boolean indicating whether the iteration is complete.
What is a key difference in how forEach and map handle array elements?
- forEach returns a new array, while map mutates the original array
- forEach is synchronous, while map is asynchronous
- forEach is purely for iteration, while map is for transformation and returns a new array
- map modifies the original array in place, while forEach creates a new array
The key difference is that forEach is primarily for iteration and doesn't return a new array, whereas map is used for transformation and creates a new array with the results of applying a function to each element of the original array.
How does the use of mixins and composition in ES6 aid in code reusability and maintainability?
- By enabling the combination of behaviors from multiple sources
- By reducing redundancy and promoting a modular approach
- By providing a way to reuse and extend classes
- By facilitating the creation of new classes with desired functionalities
In ES6, mixins and composition allow developers to combine functionalities from different sources, promoting code reusability. Mixins help in reducing redundancy, while composition enables the creation of modular and maintainable code.
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.
In a project that aims to run both on the browser and Node.js with minimal changes, how would the choice between ES6 Modules and CommonJS affect the architecture?
- ES6 Modules
- CommonJS
- No significant impact on architecture
- Depends on the specific project requirements
Choosing ES6 Modules facilitates a more seamless transition between browser and Node.js environments due to its standardized syntax and support for asynchronous module loading. CommonJS, with its synchronous nature, might require additional efforts to adapt to the asynchronous nature of browser environments.