In ES6, how is a method defined inside a class?
- Using the function keyword
- Using the method keyword
- Using the def keyword
- Using the => arrow syntax
In ES6, methods inside a class are defined using the => arrow syntax. This syntax provides a concise and cleaner way to declare methods within class definitions. It binds the method to the instance, allowing easy access to the class properties.
To check if a Map contains a certain key, use the _________ method.
- hasKey
- checkKey
- has
- includes
The correct method is has. In JavaScript, the Map object has a has method that returns a boolean indicating whether the map contains a specified key. Example: myMap.has('key').
Destructuring an object within a _________ allows for directly mapping object properties to function parameters.
- Callback function
- Template literal
- Arrow function
- Promise
Destructuring an object within an "Arrow function" allows for directly mapping object properties to function parameters. Arrow functions provide a concise syntax, and when combined with destructuring, you can streamline the mapping of object properties to function parameters.
When using Symbols for object properties, they are not included in __________ enumerations.
- for...in'
- forEach'
- Object.keys()'
- Object.getOwnPropertySymbols()'
Object properties with Symbols as keys are not included in for...in enumerations. To get all property keys (including Symbols), you can use Object.getOwnPropertySymbols() in addition to other methods. Example: const symbols = Object.getOwnPropertySymbols(myObject);
Can a default export be imported with any name?
- Yes
- No
- Only in certain cases
- Depends on the file structure
In ES6, a default export can be imported with any name. The imported name is not dictated by the exporting module. It allows flexibility in naming while importing, making it more convenient.
How can dynamic imports be used to implement feature flags in a large-scale web application?
- Load all features during the initial page load.
- Use dynamic imports to conditionally load features based on feature flags.
- Hardcode feature flags directly in the application code.
- Use static imports to include all features in the main bundle.
Dynamic imports enable the implementation of feature flags by allowing the loading of features based on runtime conditions. This flexibility helps in enabling or disabling specific features without changing the core codebase.
What is a practical use case of currying in JavaScript?
- Simplifying function composition
- Handling asynchronous operations
- Memoization
- Event handling in UI
Currying simplifies function composition by breaking down functions into smaller, reusable units. This is useful for creating more modular and maintainable code, especially in functional programming paradigms.
Imagine processing a shopping cart array to calculate the total price. Which method would efficiently achieve this?
- map
- reduce
- filter
- forEach
The most efficient method for calculating the total price in a shopping cart array is reduce. The reduce method iterates over each element of the array and accumulates a result. In this case, it can sum up the prices to provide the total cost.
How does chaining array methods like map and filter affect performance?
- Negatively impacts performance due to multiple iterations
- Positively impacts performance by optimizing the code
- No significant impact on performance
- Only affects readability, not performance
Chaining array methods like map and filter can negatively impact performance due to multiple iterations. Each method creates a new array, and chaining them leads to intermediate arrays, causing additional iterations. This can be inefficient, especially for large datasets.
In a function that accepts a variable number of arguments and needs to pass them to another function, how would the rest operator be applied?
- Declare the function parameters using the rest operator, e.g., (...args)
- Use the spread operator to pass arguments to the function
- Define the function with a fixed number of parameters
- Use the arguments object within the function
The rest operator (...) is employed in function parameters to collect a variable number of arguments into an array. For example, (...args) allows the function to accept any number of arguments and access them within an array named args.
What does the super keyword do in the context of ES6 classes?
- Calls the constructor of the parent class
- References the current class instance
- Invokes the static method of the parent class
- Provides access to the private members of the class
The super keyword in the context of ES6 classes is used to call the constructor of the parent class. It is necessary to be invoked in the child class constructor to properly set up the inherited properties.
A static method can be called without an instance of the class but not without __________ the class.
- defining
- declaring
- instantiating
- referencing
While a static method can be called without creating an instance of the class, it cannot be called without referencing the class itself. This emphasizes the association of the method with the class rather than instances.