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.
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.
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.
In functional composition, what is the result of combining two functions f and g into compose(f, g)?
- f(g(x))
- g(f(x))
- f * g
- f + g
Functional composition applies functions from right to left, so compose(f, g) means applying f after g. Therefore, the result is equivalent to f(g(x)).
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 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.
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 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.
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.
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.