In ES6, how can you define a method inside an object literal?
- Function shorthand notation
- Method shorthand notation
- Prototype method notation
- Arrow function notation
In ES6 object literals, you can define a method using the method shorthand notation. This provides a concise way to declare functions as properties of an object.
How does the super keyword differ when used in static methods compared to non-static methods?
- It behaves the same in both static and non-static methods
- It is not allowed in static methods
- It refers to the superclass in non-static and subclass in static methods
- It is used to invoke the constructor of the superclass in both cases
In a static method, 'super' refers to the parent class, while in a non-static method, it points to the parent class. This is because static methods are called on the class itself, and non-static methods are called on instances of the class.
Given a scenario where you need to flatten a deeply nested array, how would you implement this using recursion in ES6?
- Use Array.prototype.concat with recursion to flatten the array.
- Use a for...of loop to iterate through the array and flatten it.
- Implement a reduce function with recursion to flatten the array.
- Utilize the Array.prototype.flat method available in ES6.
In ES6, the reduce function can be leveraged along with recursion to flatten a deeply nested array effectively. The accumulator in reduce helps to accumulate the flattened result. This approach is versatile and applicable to various array structures. The other options either lack the necessary recursion or use methods not specifically designed for flattening deeply nested arrays.
In a scenario with a mix of synchronous and asynchronous code, how does the call stack and event loop manage the execution?
- The call stack executes synchronous code first.
- The event loop processes asynchronous code first.
- Both synchronous and asynchronous code are executed randomly.
- The call stack and event loop have no interaction.
The call stack prioritizes synchronous code, while the event loop manages the execution of asynchronous code by pushing it to the callback queue and executing it when the call stack is empty.
Can mixins in ES6 access private data of the classes they are mixed into?
- Yes, mixins have access to private data
- No, mixins cannot access private data
- It depends on the implementation of the mixin
- Private data is accessible only during class instantiation
Mixins in ES6 do not have direct access to the private data of the classes they are mixed into. Private data remains encapsulated within the class, ensuring data integrity and security.
How does method definition in ES6 classes affect the prototype chain?
- Adds methods to the subclass only
- Adds methods to the superclass only
- Does not affect the prototype chain
- Adds methods to both the subclass and superclass
In ES6 classes, method definitions impact the prototype chain. When a method is defined in a class, it gets added to the prototype of the class. When a subclass extends a superclass, its prototype chain includes both its own methods and those of the superclass. This ensures proper inheritance and method access.
What is the main advantage of using Promises over Callbacks in asynchronous JavaScript?
- Synchronous Execution
- Conciseness and Readability
- Callback hell elimination
- Procedural Approach
Promises in JavaScript provide a cleaner and more readable way to handle asynchronous code. With promises, you can avoid nested callbacks, making the code more maintainable. Option B is correct as it highlights the conciseness and readability advantages of using Promises over Callbacks.
The _________ dead zone refers to the time during which a let or const variable cannot be accessed.
- temporal
- execution
- initial
- declaration
The correct option is 'initial'. The temporal dead zone occurs between the entering of the scope and the actual declaration of the variable. During this time, trying to access the variable will result in a ReferenceError. It is called the 'initial' dead zone because it happens before the variable is initialized.
A class expression in ES6 can be named or unnamed, with the name being accessible inside the class's ________.
- Private Scope
- Protected Scope
- Public Scope
- Local Scope
The name of a class in ES6 is accessible only within the class's block scope, making it part of the class's internal structure.
In ES6, a module imported via a relative path starting with ______ indicates it is located in the same directory.
- ./
- ../
- //
- \
When using a relative path in ES6 for module imports, ./ is used to indicate that the module is in the same directory as the importing module.
How would you use a for...of loop to iterate over a string?
- for (let char in myString)
- for (let char of myString)
- for (let i = 0; i < myString.length; i++)
- for (let i = myString.length - 1; i >= 0; i--)
The correct syntax to iterate over a string using a for...of loop is for (let char of myString). This loop simplifies the process of iterating over the characters of a string without the need for an index.
Which method is used to catch errors in a Promise?
- catchError
- errorHandler
- catch
- handleError
The correct method to catch errors in a Promise is the catch method. It is used to handle both synchronous and asynchronous errors that may occur during the execution of the Promise. By chaining the catch method to a Promise, you can specify a callback function to handle the errors and take appropriate actions.