














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Rationale : Factory functions allow for the creation of private variables and methods, which are not exposed outside the function. This encapsulation is not as straightforward with constructor functions.
Rationale : The code snippet demonstrates the Module Pattern as it encapsulates private variables and methods, exposing only the necessary parts through an object.
var inside the moduleapply()Rationale : The Module Pattern uses an object return to expose public methods while keeping other methods and variables private.
Rationale : localStorage is not used to maintain private state within the function but rather for persistent browser storage. Closures, WeakMap, and Symbol are valid techniques for encapsulating state within a function.
new keyword.Rationale : Factory functions return an object and can create multiple instances without needing the new keyword.
const instance = (function() {...})(); is commonly used in the __________ pattern.Rationale : The Module Pattern helps encapsulate functionality within a single object to avoid polluting the global namespace.
this.Rationale : Arrow functions do not have their own this context and instead inherit the this context of the surrounding scope, which can be useful in factory functions.
Rationale : In the Module Pattern, you can have private methods and properties that are not exposed outside the module by only returning the necessary public methods and properties.
Rationale : Factory functions return new object instances, each with its own prototype chain, unlike constructor functions that share the same prototype.
Rationale : Closures are used to encapsulate private variables and methods within the Module Pattern, ensuring they are not accessible from outside the module.
Rationale : The Module Pattern can be more efficient in terms of memory usage because it avoids the pitfalls of classical inheritance, such as deep prototype chains.
method1: function() { return privateVar; }, method2: function() { privateVar = 'changed'; } }; } const myObject = createMyObject(); myObject.method2();
- A) 'changed' - B) 'secret' - C) undefined - D) An error ## Answer : A) 'changed' Rationale : The method `myObject.method2()` changes the value of `privateVar` to 'changed', which is returned by `method1()`. 18. Question : What does the term "revealing module pattern" refer to in JavaScript? - A) Use of `Promise` to handle asynchronous operations - B) Explicitly returning an object containing references to methods to make their names public - C) Using `class` syntax to define a module - D) Dynamically importing modules using `import()` ## Answer : B) Explicitly returning an object containing references to methods to make their names public Rationale : The revealing module pattern returns an object with method references explicitly, making it clear which methods are public. 19. Question : In ES6, how can you immediately invoke a function to create a module? - A) `const module = () => {};` - B) `import module from './module.js';` - C) `const module = (() => {})();` - D) `class Module { constructor() {} }` ## Answer : C) `const module = (() => {})();` Rationale : This syntax immediately invokes the function and assigns its return value to `module`. 2. True/False: Factory functions in JavaScript are more memory efficient than constructor functions. ## Answer: False Rationale: Factory functions can be less memory efficient than constructor functions because each object instance created by a factory function has its own copy of methods, whereas constructor functions can use prototypal inheritance to share methods across instances. 3. Fill-in-the-Blank: In the Module Pattern, an immediately invoked function expression (IIFE) is used to create a(n) _______ scope. ## Answer: private Rationale: An IIFE is used in the Module Pattern to create a private scope by executing a function immediately upon its definition, which prevents the pollution of the global scope and provides privacy. 4. Multiple Choice: Which of the following is not a feature of the Module Pattern in JavaScript? a) Private variables b) Public methods c) Inheritance d) Loose coupling ## Answer: c) Inheritance Rationale: The Module Pattern does not inherently provide inheritance, as it is a design pattern used to encapsulate private variables and expose public methods and properties. 5. True/False: The Module Pattern can be considered a form of encapsulation. ## Answer: True Rationale: The Module Pattern is indeed a form of encapsulation because it allows for the bundling of data (variables) and methods that operate on the data, while hiding the internal state of the module from the outside. 6. Fill-in-the-Blank: Factory functions can return different _______ of objects based on the input parameters. ## Answer: types Rationale: Factory functions are highly flexible and can return various types of objects tailored to specific needs, depending on the arguments passed to them. 7. Multiple Choice: What does the 'Revealing Module Pattern' primarily aim to achieve? a) Improve performance b) Make syntax more concise c) Expose only certain parts of the module d) Increase modularity ## Answer: c) Expose only certain parts of the module Rationale: The Revealing Module Pattern aims to maintain encapsulation while exposing certain properties and methods of a module as public, keeping the rest private. ## Answer: False Rationale: Factory functions can be either named or anonymous; the key feature is their ability to produce new object instances without the `new` keyword. 12. Fill-in-the-Blank: The Module Pattern utilizes closures to ensure that variables remain _______ to the outside scope. ## Answer: inaccessible Rationale: Closures allow a function to access variables from an enclosing scope even after that scope has closed, ensuring that these variables are not directly accessible from the global scope. 13. Multiple Choice: Which of the following is a benefit of using the Module Pattern? a) It can create multiple instances of a module. b) It enhances code readability and maintainability. c) It automatically binds methods to the this keyword. d) It eliminates the need for using functions altogether. ## Answer: b) It enhances code readability and maintainability. Rationale: The Module Pattern enhances code readability and maintainability by providing structure and organization, allowing developers to clearly separate concerns within the code. 14. True/False: The Revealing Module Pattern automatically exposes all variables and functions defined inside the module. ## Answer: False Rationale: The Revealing Module Pattern does not automatically expose everything; it allows the developer to choose which variables and functions to reveal publicly. 15. Fill-in-the-Blank: One of the main reasons to use factory functions over constructor functions is to avoid the issues with the 'new' keyword, such as forgetting to use it and getting an undefined _______. ## Answer: this Rationale: If the `new` keyword is omitted when calling a constructor function, `this` will be undefined within the function, leading to unexpected results and potential errors. Factory functions avoid this issue by not relying on `new`. Multiple Choice Question: Which design pattern is commonly used in JavaScript to create objects that share a set of attributes and methods? A) Singleton Pattern B) Factory Pattern C) Observer Pattern D) Prototype Pattern ## Correct Answer: B) Factory Pattern Rationale: The Factory Pattern is used to create objects by providing a generic interface that delegates the responsibility of object instantiation to its subclasses, allowing for flexibility in object creation. Fill-in-the-Blank Question: The Module Pattern in JavaScript helps in achieving _ by encapsulating private members within a closure. Fill-in-the-Blank Question: In the context of the Module Pattern, the revealing module pattern is a variation that exposes only specific _ while keeping others private. ## Correct Answer: Members Rationale: The revealing module pattern allows for selective exposure of module members, keeping certain aspects private and only revealing specific functions and variables. True/False Question: The Module Pattern in JavaScript promotes loose coupling between modules by allowing them to communicate directly with each other. ## Correct Answer: False Rationale: The Module Pattern encourages modular development by providing a way to encapsulate functionality within modules, promoting encapsulation and reducing dependencies between modules. Multiple Choice Question: Which of the following is an advantage of using factory functions in JavaScript? A) Encapsulation of private data B) Singleton object creation C) Complex inheritance hierarchies D) Tight coupling between objects ## Correct Answer: A) Encapsulation of private data Rationale: Factory functions allow for the encapsulation of private data within objects, providing data security and control by hiding internal implementation details. Fill-in-the-Blank Question: The primary benefit of using the Module Pattern in JavaScript is to avoid _ in the global namespace. ## Correct Answer: Pollution Rationale: The Module Pattern helps prevent global namespace pollution by encapsulating functionality within modules, reducing the risk of naming collisions and conflicts. True/False Question: In the context of factory functions, each object created shares the same prototype chain. ## Correct Answer: False Rationale: Factory functions allow for the creation of objects with their own unique prototype chains, enabling each object to have its own set of properties and methods. Multiple Choice Question: Which design pattern promotes the creation of objects without the use of a constructor function in JavaScript? A) Observer Pattern B) Singleton Pattern C) Factory Pattern D) Prototype Pattern ## Correct Answer: C) Factory Pattern Rationale: The Factory Pattern promotes object creation without the need for constructor functions, providing a more flexible approach to creating objects.