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.

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.

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.

What is the main purpose of default parameters in ES6 functions?

  • Enhancing Function Flexibility
  • Enabling Type Annotations
  • Facilitating Function Overloading
  • Simplifying Function Parameter Handling
Default parameters in ES6 functions provide a way to set default values for function parameters, enhancing flexibility by allowing functions to be called with fewer arguments or without any arguments. This can simplify code and handle different use cases without requiring explicit checks for undefined values.

The _________ method is used to execute a function on each item in a Set.

  • forEach
  • execute
  • loop
  • iterate
The correct method is forEach. The forEach method in Set allows you to execute a provided function once for each element in the Set, in insertion order. Example: mySet.forEach((value) => console.log(value));.