从同一源导入的功能是否可以访问其源全局变量?

发布于 2025-02-03 07:55:40 字数 857 浏览 5 评论 0原文

好的,我使用我知道的每个关键字搜索了我知道的每个站点,但是我发现没有什么可以关闭的我搜索的主题,也许整个问题是错误的,但是无论如何...

假设我们有一个名为index.js的模块,其中包含存储和两个负责读取/写入存储的功能。

var STORAGE = []

function writeStorage(data) {
  STORAGE.push(data)
}

function readStorage(data) {
  //find data in STORAGE... and return result as RES
  return RES
}

现在,在名为write.js的文件中,我们导入writestorage()

在名为read.js的另一个文件中,我们导入readstorage()

这就是问题上升的地方,

是否会从同一源导入功能,可以访问其源全局变量吗?

ie,我们可以在index中修改storege。 JS,使用writestorage()我们在write.js中导入的?

并读取存储使用read.js使用readstorage()?。

OK i searched every site i knew, using every keyword i knew, but i found nothing close the subject i searched about, maybe the whole question is wrong, but anyway...

let's say we have a module named Index.js, which contains a STORAGE and two functions responsible for reading/writing to STORAGE.

var STORAGE = []

function writeStorage(data) {
  STORAGE.push(data)
}

function readStorage(data) {
  //find data in STORAGE... and return result as RES
  return RES
}

Now, inside a file named write.js, we import writeStorage().

While in another file named read.js, we import readStorage().

And this is where the question rises,

Does imported functions from the same source, have access to their source global variables ?

i.e, can we modify STORAGE in index.js, using writeStorage() which we imported in write.js ?

and read STORAGE changes from read.js using readStorage() ?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2025-02-10 07:55:40

我想为此提供更清晰的答案。功能有两个范围:词汇和动态。 动态表示,一个函数将在该区域中搜索变量,该变量被称为:

let someVar = 'global';

function dynamicScope () {
    someVar = 'dynamic';
    console.log(somevar);
}

dynamicalScope(); // will display 'global'

词汇表示,该函数将搜索变量,而变量是< Strong>书面/声明:

let someVar = 'global';

function lexicalScope () {
    someVar = 'lexical';
    console.log(somevar);
} 

lexicalScope(); // will display 'lexical'

但是在JavaScript中,无法使用动态范围,因此所有函数都将搜索变量 已声明/书面

不要注意细节,这只是一个伪代码。

I want to provide clearer answer about that. There are two scopes for functions: lexical, and dynamic. Dynamic means, a function will search variables in the area, where it was called:

let someVar = 'global';

function dynamicScope () {
    someVar = 'dynamic';
    console.log(somevar);
}

dynamicalScope(); // will display 'global'

Lexical means, the function will search variables where it was written/declared:

let someVar = 'global';

function lexicalScope () {
    someVar = 'lexical';
    console.log(somevar);
} 

lexicalScope(); // will display 'lexical'

But in JavaScript, there is no way to use dynamic scope, so all function will search variables where it has been declared/written.

DO NOT PAY ATTENTION ON DETAILS, IT'S JUST A PSEUDO-CODE.

小傻瓜 2025-02-10 07:55:40

简短答案:是的,这是可能的。

Short answer: Yes, that's possible.

酷炫老祖宗 2025-02-10 07:55:40

是的,您可以做到这一点。

这篇文章也可能有助于理解:
相同的JavaScript分享相同的词汇范围

另一个例子是证明@Azamat的答案

//file_to_import.js
let fruit = 'banana';
let fruits = []

export function describeFruit(){
    console.log(`The ${fruit} is yellow`)
}

export function listFruits () {
    console.log(`Fruits: ${fruits.join(', ')}`)
}

export function addFruits (fruitToAdd) {
    fruits.push(fruitToAdd)
}


setTimeout(listFruits, 1000)
setTimeout(listFruits, 5000)



//index.js
import { addFruits, describeFruit} from "./file_to_import.js"; //import brings in functions and initializes (runs) all code from the file_to_import.js

//Declare a variable named 'fruit' .  There now exists two fruit variables - one in the scope of this file (index.js) which is the string 'apple' and the other in the scope of the imported file (file_to_import) which is the string 'banana'.
let fruit = 'apple'

//Demonstrate the imported function has access to the variables in the scope from which it was declared
describeFruit() // logs 'the banana is yellow' demonstrating the access to the variable 'fruit' in file_to_import.js NOT to the variable fruit as declared here


// Code from 1st setTimeout (1000ms delay) from file_to_import executes, showing we have no fruits in our 'fruits' array as declared in file_to_import
// Code from this setTimeout (3000ms delay) executes, adding the variable fruit, as it exists in the scope of this file, to the array 'fruits' as it is exists in the file_to_import scope
setTimeout(()=>addFruits(fruit), 3000); 
//Code from 2nd setTimeout (5000ms delay) from file_to_import executes showing we now have an apple in our array

Yes you can do this.

This post might also help in understanding:
Same javascript module imported in different files, sharing the same lexical scope

A further example to demonstrate @Azamat 's answer

//file_to_import.js
let fruit = 'banana';
let fruits = []

export function describeFruit(){
    console.log(`The ${fruit} is yellow`)
}

export function listFruits () {
    console.log(`Fruits: ${fruits.join(', ')}`)
}

export function addFruits (fruitToAdd) {
    fruits.push(fruitToAdd)
}


setTimeout(listFruits, 1000)
setTimeout(listFruits, 5000)



//index.js
import { addFruits, describeFruit} from "./file_to_import.js"; //import brings in functions and initializes (runs) all code from the file_to_import.js

//Declare a variable named 'fruit' .  There now exists two fruit variables - one in the scope of this file (index.js) which is the string 'apple' and the other in the scope of the imported file (file_to_import) which is the string 'banana'.
let fruit = 'apple'

//Demonstrate the imported function has access to the variables in the scope from which it was declared
describeFruit() // logs 'the banana is yellow' demonstrating the access to the variable 'fruit' in file_to_import.js NOT to the variable fruit as declared here


// Code from 1st setTimeout (1000ms delay) from file_to_import executes, showing we have no fruits in our 'fruits' array as declared in file_to_import
// Code from this setTimeout (3000ms delay) executes, adding the variable fruit, as it exists in the scope of this file, to the array 'fruits' as it is exists in the file_to_import scope
setTimeout(()=>addFruits(fruit), 3000); 
//Code from 2nd setTimeout (5000ms delay) from file_to_import executes showing we now have an apple in our array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文