What happens when a generator function encounters a yield keyword?
- Pauses the function execution
- Throws an error
- Skips the yield keyword
- Resumes execution from the last yield
When a generator function encounters a yield keyword, it pauses the execution of the function, preserving its state. The function can later be resumed from the last yield encountered, allowing for efficient iteration over a sequence of values.
When exporting a class or function as a default export, it's not necessary to use the ________ keyword.
- export default
- default
- export
- class
When exporting a class or function as a default export, you can simply use export default followed by the class or function declaration without explicitly using the function or class keywords.
Higher-order functions that alter the execution context of another function are often referred to as __________.
- Decorators
- Generators
- Closures
- Combinators
Decorators in JavaScript are higher-order functions that alter the behavior or execution context of another function. They are often used to add functionalities to functions or methods.
How do you import a default export from a module?
- Use the import keyword followed by any name of your choice, as the default export has no specific name.
- Use the import default statement followed by the name specified in the exporting module.
- Use the import { default } from 'module'; syntax.
- Use the import * as alias from 'module'; syntax.
To import a default export from an ES6 module, you use the import keyword followed by any name of your choice, as the default export does not have a specific name. The imported name serves as an alias for the default export in your code.
Q2: Given a scenario where a subclass needs to modify a method inherited from a parent class, how would you implement this in ES6?
- Use the 'extends' keyword
- Utilize the 'super' keyword
- Override the method in the subclass
- Employ the 'Object.create()' method
In ES6, you can implement method overriding by defining the method with the same name in the subclass. This allows the subclass to provide a specialized implementation while still inheriting from the parent class.
In a Promise-based AJAX call, ________ is used to asynchronously await the response without blocking the execution.
- await
- then
- catch
- async
In a Promise-based AJAX call, the await keyword is used to asynchronously await the response without blocking the execution. This allows the code to continue its execution while waiting for the AJAX response.
Can the spread operator be used to combine two arrays into one in ES6?
- Yes
- No
- Only if the arrays are of the same length
- Only if the arrays are of different types
Yes, the spread operator can be used to combine two arrays into one by spreading the elements of both arrays into a new array. It's a concise way of merging arrays without modifying the original arrays.
In ES6, how can the spread operator (...) be used in a recursive function?
- Using the spread operator in a recursive function allows for the expansion of an array or iterable, enabling the passing of individual elements as arguments. This can be useful when dealing with variable argument lists in recursive calls.
- The spread operator can be employed to concatenate arrays, ensuring a more efficient handling of recursive calls where arrays need to be merged or combined.
- It is possible to use the spread operator to clone objects within a recursive function, providing a mechanism to duplicate complex data structures without modifying the original objects.
- Utilizing the spread operator in a recursive function helps to flatten nested arrays, simplifying the processing of deeply nested structures in the recursive calls.
The correct usage of the spread operator in a recursive function involves spreading the array or iterable inside the function's argument list. This is particularly helpful when dealing with functions that accept a variable number of arguments, allowing for flexibility in recursive calls.
When used in a function argument, what does the rest operator do with the supplied arguments?
- Gathers them into an array
- Ignores them
- Converts them to a string
- Spreads them to all variables
The rest operator (...) in a function parameter allows you to gather all the remaining arguments into a single array. This is helpful when you have a variable number of arguments and want to process them as an array within the function.
Can a class in ES6 contain constructor functions?
- Yes
- No
- Only if explicitly declared
- Only if it extends another class
Yes, a class in ES6 can contain a constructor function. The constructor is a special method that gets called when an object is instantiated from the class. It is commonly used for initializing object properties.
What is the difference between defining methods in ES5 and ES6 object literals?
- Function Declaration
- Shorthand Syntax
- Prototype Extension
- Arrow Functions
In ES6 object literals, the method definition can be written using shorthand syntax, avoiding the need for the function keyword. This not only makes the code more concise but also automatically assigns a non-enumerable property to the method, leading to improved performance.
In a scenario involving an e-commerce site, how can currying be used to handle different types of discounts for products?
- Simplifying Discount Logic and Promoting Reusability
- Increasing Code Complexity and Redundancy
- Centralizing Discount Calculation in a Single Function
- Improving Security and Authorization for Discounts
Currying enables breaking down the discount calculation into partial functions, making the logic more modular. Each partial function can handle a specific type of discount. This approach simplifies the discount management system, promotes code reusability, and allows for easier maintenance. Centralizing discount logic with currying enhances the overall flexibility and maintainability of the e-commerce application.