What happens when undefined is passed to a function with a default parameter?
- The default parameter is used
- An error is thrown
- The function assigns a default value
- It takes the value 'undefined'
When 'undefined' is passed, the default parameter is used. Explanation should cover how default parameters handle 'undefined' and when they come into play.
Which method is used to handle errors in a JavaScript Promise?
- catch()
- reject()
- error()
- handleError()
The catch() method is used to handle errors in a JavaScript Promise. It allows you to specify a callback function that will be executed if the promise is rejected. Option A is correct because it represents the correct method for handling errors in promises.
Can getter and setter functions be defined within ES6 object literals?
- Yes, both getter and setter functions can be defined.
- No, only getter functions are allowed.
- No, only setter functions are allowed.
- No, neither getter nor setter functions are allowed.
In ES6 object literals, both getter and setter functions can be defined using the concise method syntax. This feature allows developers to encapsulate access to object properties, providing a cleaner and more controlled way to retrieve and set values. Using getter and setter functions within object literals enhances the encapsulation of data and promotes a more modular code structure.
To optimize tree shaking, developers should avoid _______ side effects in their module code.
- Pure
- Impure
- Global
- Conditional
Tree shaking relies on the ability to analyze and remove dead code. Side effects in module code, especially impure ones, can hinder the accuracy of tree shaking. Developers are encouraged to write "pure" functions and modules, which have no side effects and make tree shaking more effective.
The __________ file in a module directory helps in defining the entry point for the module in ES6.
- main.js
- module.js
- index.js
- entry.js
In ES6, the index.js file serves as the default entry point for a module within a directory. It is automatically recognized when importing the module.
How can enhanced object literals in ES6 improve the process of creating objects that inherit from another object?
- Facilitates concise syntax for defining the prototype chain
- Enables automatic inheritance through the extends keyword
- Introduces mixins for flexible composition of properties
- Provides a dedicated Object.extend method for inheritance
Enhanced object literals in ES6 simplify the process of creating objects that inherit from another object by providing a concise syntax for defining the prototype chain. This concise syntax enhances code readability and reduces the boilerplate code associated with prototype-based inheritance, making it more accessible and efficient.
In a scenario where you need to create multiple objects with similar properties and methods, how would you utilize ES6 classes to achieve this?
- Prototypal Inheritance
- Object Factories
- Constructor Functions
- ES6 Classes
In ES6, classes provide a cleaner syntax for creating objects with shared properties and methods. Options A, B, and C are also ways to achieve object creation, but ES6 classes offer a more concise and structured approach.
Arrow functions do not have their own this keyword; instead, they ________ the this value from the enclosing execution context.
- Absorb
- Inherit
- Borrow
- Capture
Arrow functions in JavaScript do not have their own this context. Instead, they inherit the this value from the enclosing execution context. This behavior is different from regular functions, which have their own this binding.
What are the limitations of destructuring assignment when dealing with complex data structures?
- Limited support for nested structures
- Inability to rename variables
- Difficulty in handling arrays
- All of the above
Destructuring assignment may face limitations with complex nested structures, making it challenging to destructure deeply nested objects or arrays. Additionally, it lacks the ability to rename variables, and handling arrays can be trickier in certain scenarios.
What is the purpose of the WeakMap and WeakSet in ES6 compared to Map and Set?
- They have better performance characteristics for certain use cases.
- They are used for storing weakly held references to objects.
- They have additional methods for manipulating keys and values.
- They have stricter key equality rules.
WeakMap and WeakSet are designed for scenarios where you want to associate additional data with objects without preventing garbage collection. They are used for weakly held references.