admin管理员组文章数量:1327665
I have written my code across several files for my node server.
If I have a file, say basket.js:
var Basket = {
fruits : 0,
addFruit : function() {
fruits++;
},
removeFruit : function() {
fruits--;
},
printFruit : function() {
console.log(this.fruits);
}
}
module.export = Basket;
And I have another file called give.js:
var Basket1 = require("./basket.js");
Basket1.addFruit();
Basket1.printFruit();
And another file called take.js:
var Basket2 = require("./basket.js");
Basket2.removeFruit();
Basket2.printFruit();
Will both files write into the same instance of Basket? In other words, will they both have control over the property, fruits? Does node manage race conditions on its own? i.e. if two mands to modify fruit e in at the same time from add and sub, does node know how to handle it?
If I want to make a way in which two files can look at a singleton at the same time and access it, is this the way to go?? Or how else does one do it?
I have written my code across several files for my node server.
If I have a file, say basket.js:
var Basket = {
fruits : 0,
addFruit : function() {
fruits++;
},
removeFruit : function() {
fruits--;
},
printFruit : function() {
console.log(this.fruits);
}
}
module.export = Basket;
And I have another file called give.js:
var Basket1 = require("./basket.js");
Basket1.addFruit();
Basket1.printFruit();
And another file called take.js:
var Basket2 = require("./basket.js");
Basket2.removeFruit();
Basket2.printFruit();
Will both files write into the same instance of Basket? In other words, will they both have control over the property, fruits? Does node manage race conditions on its own? i.e. if two mands to modify fruit e in at the same time from add and sub, does node know how to handle it?
If I want to make a way in which two files can look at a singleton at the same time and access it, is this the way to go?? Or how else does one do it?
Share Improve this question edited Dec 30, 2020 at 11:21 Edoardo 5,5221 gold badge29 silver badges34 bronze badges asked Jul 29, 2014 at 12:34 user3794529user3794529 772 silver badges6 bronze badges 3- 1 What happens when you try it out? – pattmorter Commented Jul 29, 2014 at 12:38
- When I try out the above code, it does'nt work... it creates a new instance of Basket every time i run removeFruit or printFruit... – user3794529 Commented Jul 29, 2014 at 14:40
- You may want to take a look at this another answer. – Edwin Dalorzo Commented Jul 29, 2014 at 19:19
2 Answers
Reset to default 5Yes, they will access the same object.
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.
– Modules docs
No, node does not manage race conditions on its own, because race conditions will not be caused by node itself. Node is single-threaded and thus no code can be executed at the same time as other code. See for example this answer for some more explanation.
I'm a beginner but I think the correct syntax is module.exports
not modules.export
- if you may correct so that people don't wonder why it does not work like I just did :)
本文标签: javascriptNodejs Global variables across multiple filesStack Overflow
版权声明:本文标题:javascript - Nodejs: Global variables across multiple files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223501a2435602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论