From fcb0fdb9b5c6ff5480a36d5ac1dbcafdec142154 Mon Sep 17 00:00:00 2001 From: Jenesh Date: 2025年1月18日 13:35:38 +0530 Subject: [PATCH 1/2] Added new JavaScript questions and solutions to README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index cd7fe811..a7354db9 100644 --- a/README.md +++ b/README.md @@ -518,6 +518,8 @@ | 468 | [How to find the number of parameters expected by a function?](#how-to-find-the-number-of-parameters-expected-by-a-function) | | 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) | | 470 | [What are the array mutation methods?](#what-are-the-array-mutation-methods) | +| 471 | [What is module scope in JavaScript?](#what-is-module-scope-in-JavaScript) | + @@ -8950,8 +8952,47 @@ The `globalThis` property provides a standard way of accessing the global object **[⬆ Back to Top](#table-of-contents)** +471. ### What is module scope in JavaScript? + Module scope is a feature introduced with ES6 (ES2015) modules that creates a scope specific to a module file, isolating variables and functions declared within it from the global scope and other modules. Variables and functions declared in a module are private by default and can only be accessed by other modules if they are explicitly exported. + + Key characteristics of module scope: + + 1. Variables declared in a module are scoped to that module only. + 2. Each module has its own top-level scope + 3. Variables and functions need to be explicitly exported to be used in other modules + 4. The global scope cannot access module variables unless they are explicitly exported and imported + 5. Modules are always executed in strict mode + + ```javascript + // moduleA.js + const privateVariable = "I am private"; + export const publicVariable = "I am public"; + + export function publicFunction() { + console.log(privateVariable); // Works - can access private variables + return "Public function"; + } + + // moduleB.js + import { publicVariable, publicFunction } from './moduleA.js'; + + console.log(publicVariable); // "I am public" + console.log(privateVariable); // ReferenceError: privateVariable is not defined + + ``` + Common use cases and benefits: + + - Encapsulation of module-specific code + - Prevention of global scope pollution + - Better code organization and maintenance + - Explicit dependency management + - Protection of private implementation details + +**[⬆ Back to Top](#table-of-contents)** + + ### Coding Exercise #### 1. What is the output of below code From 7e9dbeb84b085238f5708ccd184034b553f2bdbe Mon Sep 17 00:00:00 2001 From: Jenesh Date: 2025年1月18日 14:51:46 +0530 Subject: [PATCH 2/2] Added new JavaScript questions and solutions to README --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a7354db9..6901da9c 100644 --- a/README.md +++ b/README.md @@ -519,7 +519,6 @@ | 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) | | 470 | [What are the array mutation methods?](#what-are-the-array-mutation-methods) | | 471 | [What is module scope in JavaScript?](#what-is-module-scope-in-JavaScript) | - @@ -8992,7 +8991,6 @@ The `globalThis` property provides a standard way of accessing the global object - ### Coding Exercise #### 1. What is the output of below code

AltStyle によって変換されたページ (->オリジナル) /