admin管理员组文章数量:1287488
I am seeing this pattern these days for nodeJS implementation, where in we have the following code for my Module:
class Foo {
bar() {
console.log('bar');
}
}
module.exports = Foo;
Then when I do a require and finally say new to create an instance of the class.
var Foo = require(./foo);
var myFoo = new Foo();
myFoo.bar();
This pattern according to me will keep creating multiple instances of class Foo, everytime it is called.
The other pattern could have been what i am used to where in my foo.js would be.
module.exports = {
bar: function() {
console.log('bar');
}
};
And then i just require and call bar.
var foo = require(./foo);
foo.bar();
Question would these two behave the same? Will the second pattern keep creating objects everytime I require and call bar on the object?
Isn't creating singleton pattern a good idea in case 1 as we are used to in other languages, wherein we keep serving the same instance if it has already been created once?
I am seeing this pattern these days for nodeJS implementation, where in we have the following code for my Module:
class Foo {
bar() {
console.log('bar');
}
}
module.exports = Foo;
Then when I do a require and finally say new to create an instance of the class.
var Foo = require(./foo);
var myFoo = new Foo();
myFoo.bar();
This pattern according to me will keep creating multiple instances of class Foo, everytime it is called.
The other pattern could have been what i am used to where in my foo.js would be.
module.exports = {
bar: function() {
console.log('bar');
}
};
And then i just require and call bar.
var foo = require(./foo);
foo.bar();
Question would these two behave the same? Will the second pattern keep creating objects everytime I require and call bar on the object?
Isn't creating singleton pattern a good idea in case 1 as we are used to in other languages, wherein we keep serving the same instance if it has already been created once?
Share Improve this question edited Sep 16, 2016 at 9:31 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Sep 16, 2016 at 8:56 Sourav ChatterjeeSourav Chatterjee 8102 gold badges14 silver badges29 bronze badges 2- you are using ES6 so be sure that, very few browser support this feature. – arjun kori Commented Sep 16, 2016 at 9:01
- The intention is to run in nodeJS and not in browser. – Sourav Chatterjee Commented Sep 16, 2016 at 9:29
1 Answer
Reset to default 10This pattern according to me will keep creating multiple instances of class Foo, everytime it is called.
Yes, that's the purpose. Though new Foo();
is only executed once in your example as well.
would these two behave the same?
No.
Will the second pattern keep creating objects everytime I require and call bar on the object?
No. require()
calls are cached, they return the same module every time.
Isn't creating singleton pattern a good idea?
No, a singleton is rarely a good idea if it contains any state. Your example does not, but where class
es are used in Node.js they usually do.
本文标签: javascriptES6 Class and moduleexportsStack Overflow
版权声明:本文标题:javascript - ES6 Class and module.exports - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741305161a2371323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论