Understanding Event Delegation
In JavaScript, which statement best describes event delegation, particularly when handling many dynamic child elements within a list?
- Attaching a single event listener to a parent node to handle events from its current and future child elements.
- Adding an event listener to every child node explicitly once during initialization.
- Assigning event listeners to the window object to listen to all events globally.
- Using only inline HTML event handlers for every child dynamically.
- Calling preventDefault in every child node’s event handler.
The 'this' Keyword Complexity
Inside a JavaScript function (not in strict mode) called as a plain function (not as a method or with bind/call/apply), what does 'this' refer to by default?
- The global object (e.g., window in browsers)
- The function’s prototype
- The child scope object
- The parent object of where the function is defined
- The undefined value
Prototypal Inheritance Explained
Which statement correctly explains how prototypal inheritance works in JavaScript?
- Objects inherit properties by establishing an internal link to another object called its prototype.
- Every object copies all properties from its parent at creation time.
- Inheritance in JavaScript is only possible using classes introduced in ES6.
- Inheritance in JavaScript relies solely on the 'super' keyword.
- Only native objects participate in the prototype chain.
AMD vs CommonJS Distinctions
Given a JavaScript module system scenario, which statement best describes the fundamental difference between AMD and CommonJS?
- AMD is optimized for loading modules asynchronously in browsers; CommonJS is designed for synchronous loading in server environments.
- AMD stands for All Module Definitions, while CommonJS does not involve modules.
- CommonJS is only used for CSS files, while AMD applies to JavaScript.
- Both require all dependencies to be loaded before any code executes.
- AMD cannot define dependencies explicitly.
Immediate Invoked Function Expressions (IIFEs)
Why does 'function foo() { }();' not work as an IIFE in JavaScript, and what needs to be changed to make it a valid IIFE?
- It’s a function declaration, not an expression; for a valid IIFE, wrap the function in parentheses like (function(){})();
- It lacks the var keyword before foo; add 'var' to fix it.
- JavaScript does not support automatically invoked functions.
- The syntax is correct, no changes needed.
- Functions must be assigned to objects before calling them automatically.
Variable State Differences
Given 'var a;', 'var b = null;', and an undeclared variable 'c', which JavaScript statement is true?
- 'a' is undefined, 'b' is null, 'c' causes a ReferenceError when accessed.
- 'a' is null, 'b' is undefined, 'c' is null.
- 'a' is undeclared, 'b' is undefined, 'c' is undefined.
- 'a' and 'b' are both undefined, 'c' causes a SyntaxError.
- 'a' is declared and initialized, 'b' is undeclared, 'c' is null.
Closures and Scope
How does a closure in JavaScript maintain access to variables after the outer function has finished execution? Consider a scenario where an inner function references an outer variable.
- By storing a reference to the outer scope's variables in memory, even after the outer function returns.
- By copying all outer variables into the inner function at definition time.
- By rewriting the variable values in the global scope.
- Through hoisting all inner variable definitions to the outer function.
- Closures do not allow access to outer variables after function execution.
Anonymous Functions Usage
In which scenario are anonymous functions particularly useful in JavaScript?
- As callbacks passed to higher-order functions like map, filter, or event handlers.
- For declaring named constructors only.
- When defining global constants.
- For use only with the 'for' loop statement.
- Anonymous functions are not allowed in JavaScript.
Host vs Native Objects
Which distinction best characterizes host objects versus native objects in JavaScript?
- Host objects are provided by the environment (e.g., 'window' in browsers), while native objects are part of the ECMAScript standard (e.g., Array, String).
- Native objects depend on the host environment and can be redefined.
- Host objects are only available in Node.js, while native objects are only in browsers.
- There is no difference; all JavaScript objects are host objects.
- Host objects are defined in user code using the 'host' keyword.
Function Construction and Invocation
Consider the following patterns: 'function Person() {}', 'var person = Person()', and 'var person = new Person()'. What is the correct explanation of these three statements?
- 'Person()' declares a function; 'person = Person()' invokes it directly (returns undefined); 'person = new Person()' creates a new instance with 'Person' as its constructor.
- 'Person()' is an object; 'person = Person()' creates a new prototype; 'person = new Person()' invokes Person without any context.
- All three statements declare new objects with the same prototype chain.
- 'Person()' creates a variable; 'person = Person()' declares the function; 'person = new Person()' assigns the prototype to person.
- None of the statements are valid in JavaScript syntax.