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.

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.

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.

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.

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.

What is the role of the .catch() method in Promise chaining?

  • It is used to handle errors that occur in the promise chain.
  • It is only applicable to synchronous code.
  • It can only be used once in a promise chain.
  • It must always be placed before any .then() block.
The .catch() method in Promise chaining is used to handle errors that occur anywhere in the promise chain. It ensures that errors are caught and appropriate actions are taken to handle them gracefully.

The spread operator can be effectively used to concatenate arrays without the use of traditional methods like '__________'.

  • concat
  • push
  • join
  • spread
The spread operator (...) can be used to concatenate arrays in a concise way. It allows you to spread the elements of one array into another, effectively achieving concatenation without using traditional methods like concat, push, or join.

How would you use static properties in a class representing a database connection to ensure there is only one connection instance?

  • Implement a singleton pattern by allowing only one instance of the class and store the connection as an instance property.
  • Utilize a static property to store the connection instance and check its existence before creating a new one.
  • Use a separate connection manager class with a static property to handle the singleton behavior.
  • Implement a global variable to store the connection instance and update it as needed.
To ensure there is only one instance of a database connection, using a static property within the class is appropriate. This static property can store the connection instance, and before creating a new connection, you can check if the static property already holds a valid connection. This approach aligns with the singleton pattern, ensuring a single point of access to the connection.

How does the inclusion of "type": "module" in the package.json affect module resolution in Node.js?

  • Enables ECMAScript module resolution
  • Disables CommonJS module resolution
  • Triggers automatic transpilation of modules
  • Specifies module entry points
Including "type": "module" in package.json informs Node.js that the project uses ECMAScript modules. This affects module resolution by enabling the ECMAScript module resolution strategy, allowing the use of import and export statements.

Consider a situation where you need to execute multiple asynchronous operations in sequence, each dependent on the result of the previous one. How would you implement this with Promises?

  • Chaining Promises with .then()
  • Promise.sequence()
  • Promise.all()
  • Async/Await
To execute multiple asynchronous operations in sequence, you would chain promises using .then(). This ensures that each operation is dependent on the result of the previous one. This approach maintains a clear and sequential flow in your asynchronous code.