Which of these methods allows you to explicitly set the this
value when calling a function in JavaScript?
Explanation:
The .call()
method allows you to invoke a function with a specified this
value and individual arguments provided separately. apply()
is similar but takes an array of arguments.
What will be logged to the console in this code snippet: console.log((function(a) { return a * a; })(5));
?
Explanation:
This code demonstrates an immediately invoked function expression (IIFE). The function squares the input and is executed immediately with the argument 5, resulting in 25 being logged.
What is a common practice to avoid deeply nested callbacks when working with multiple asynchronous operations?
Explanation:
Promise chaining provides a way to structure asynchronous code sequentially without nesting callbacks deeply. By chaining '.then()' methods, the result of one asynchronous operation is passed to the next, improving readability and maintainability.
What is a common way to achieve encapsulation in JavaScript when modules are not an option (e.g., in older environments)?
Explanation:
Closures provide a way to emulate private members in JavaScript. Functions retain access to their lexical scope, even after the outer function has finished executing. This allows inner functions (within the closure) to act as getters and setters for variables defined in the outer function's scope, effectively hiding them from direct external access.
What is the purpose of the .catch()
method in Promise chaining?
Explanation:
The .catch()
method is crucial for handling errors in Promise chains. It executes when a Promise in the chain rejects, allowing you to gracefully handle the error.
Which principle of object-oriented programming focuses on hiding internal implementation details and exposing only necessary information?
Explanation:
Encapsulation, a core OOP concept, involves bundling data (properties) and methods that operate on that data within an object, protecting the internal workings from external access.
Which of the following methods is used to handle errors within an 'async/await' function?
Explanation:
While .catch()
is used with Promises, the most appropriate way to handle errors specifically within an 'async/await' function is by using a standard 'try...catch' block. This allows for structured error handling within the asynchronous code.
Which method is most efficient for finding a specific element in the DOM based on its ID?
Explanation:
getElementById
is the most efficient method for finding an element by its ID. The DOM specifically indexes elements by ID for this purpose, making it much faster than other methods.
In JavaScript, how does the prototype chain relate to object inheritance?
Explanation:
When accessing an object's property or method, JavaScript traverses the prototype chain. If not found directly on the object, it checks the object's prototype, then its prototype, and so on, until found or reaching the end of the chain.
What is the primary difference between 'nextSibling' and 'nextElementSibling' in DOM traversal?
Explanation:
The key difference is that 'nextElementSibling' skips over text nodes, comments, or other non-element nodes, while 'nextSibling' might return any type of node that is the next sibling.