In ES6, ________ methods are methods that are shared among all instances of a class.
- Static
- Prototype
- Instance
- Constructor
In ES6, static methods are associated with the class itself and not with instances. They are shared among all instances of the class.
What is the implication of using arrow functions in constructors?
- No implication
- this' is not bound to the instance
- this' is bound to the instance
- this' is bound to the constructor function
Arrow functions do not bind their own 'this', and using them in constructors can lead to 'this' being bound to the enclosing scope rather than the instance being created. This can result in unexpected behavior, making it not recommended to use arrow functions in constructors.
How are escape sequences like n treated in template literals?
- Treated as a newline character
- Ignored
- Treated as a literal backslash followed by 'n'
- Throws an error
In template literals, escape sequences like n are treated as newline characters. This allows for multi-line strings without the need for explicit line break characters.
In what order are tasks executed given JavaScript’s event loop and call stack mechanism?
- Based on the complexity of the task
- First In, First Out (FIFO) order
- Random order
- Last In, First Out (LIFO) order
Tasks in JavaScript are executed in a First In, First Out (FIFO) order. The event loop continually checks the call stack and executes tasks in the queue. This order ensures that tasks are processed in the sequence they are received, maintaining the integrity of the program's logic.
In a scenario where you need to convert an array of user objects into an array of names, which array method is the most suitable?
- map
- filter
- reduce
- forEach
In this scenario, the most suitable array method is map. The map method is designed for transforming each element of an array and returning a new array with the transformed values. It allows you to extract the names from the user objects efficiently.
When using Promise.all(), if any promise is rejected, the entire operation is considered _________.
- resolved
- pending
- rejected
- fulfilled
If any promise in the iterable passed to Promise.all() is rejected, the entire operation is considered rejected. All other promises are ignored, and the Promise.all() itself is rejected with the reason of the first rejected promise.
Arrow functions should not be used as ________ because they cannot be used with the new operator.
- Constructors
- Event Listeners
- Callbacks
- Promises
Arrow functions are not suitable for use as constructors since they lack a prototype property and cannot be used with the new operator. They are primarily designed for concise function expressions.
Q3: Consider an application that monitors the status of DOM elements. How would a WeakSet be beneficial in this context?
- WeakSet
- Set
- Both WeakSet and Set
- Neither WeakSet nor Set
In the context of monitoring DOM element status, a WeakSet is beneficial. This is because a WeakSet can hold references to DOM elements without preventing them from being garbage collected when they are removed from the DOM. As DOM elements are added and removed dynamically, a WeakSet ensures that the references do not cause memory leaks. If a DOM element is removed from the document, its reference in the WeakSet becomes automatically eligible for garbage collection. A Set, on the other hand, would keep references even if the DOM elements are removed, potentially leading to memory issues in the long run.
Mixins in ES6 are commonly used for adding _________ to classes without using inheritance.
- Properties
- Methods
- Behavior
- Variables
Mixins in ES6 are commonly used for adding behavior (methods) to classes without using traditional inheritance. This approach enhances code flexibility and avoids the potential downsides of deep class hierarchies.
What is the primary purpose of the spread operator in ES6?
- Spreading elements of an array or object
- Merging two arrays
- Concatenating strings
- Creating a new variable
The spread operator (...) is primarily used to spread elements of an array or object, allowing them to be expanded in places where multiple elements or variables are expected. This can be useful for passing array elements as individual arguments to a function or merging arrays.