admin 管理员组文章数量: 1087134
I'm trying to assign a constructor in a self-executing function in NodeJS. I'm pretty sure it's not working because my parameter is a variable pointing to module.exports, but I'm curious if there's a way to make it work while staying as close to the self-executing format as possible.
Here's how the code is being called...
var TemplateEngine = require('./templateEngine');
templateEngine = new TemplateEngine({engine: 'swig'}); // "object is not a function"
Here's an example of code that works fine...
var assert = require('assert');
var swig = require('swig');
// Constructor
var TemplateEngine = function(args) {
assert.ok(args.engine, 'engine is required');
var templateEngine = {};
templateEngine.engine = args.engine;
templateEngine.Render = function(templateString, model) {
var result = swig.render(templateString, model);
return result;
};
return templateEngine;
};
module.exports = TemplateEngine;
and here's an example of the code style I'd like to use, but which produces a "TypeError: Object is not a function" error because I'm not actually assigning to module.exports, just a variable that copied whatever it was pointing to.
(function(templateEngine) {
var assert = require('assert');
var swig = require('swig');
templateEngine = function(args) {
assert.ok(args.engine, 'engine is required');
var templateEngine = {};
templateEngine.engine = args.engine;
templateEngine.Render = function (templateString, model) {
var result = swig.render(templateString, model);
return result;
};
return templateEngine;
};
})(module.exports);
Is there a way for me to use the above self-executing format and have my module export a Constructor?
I'm trying to assign a constructor in a self-executing function in NodeJS. I'm pretty sure it's not working because my parameter is a variable pointing to module.exports, but I'm curious if there's a way to make it work while staying as close to the self-executing format as possible.
Here's how the code is being called...
var TemplateEngine = require('./templateEngine');
templateEngine = new TemplateEngine({engine: 'swig'}); // "object is not a function"
Here's an example of code that works fine...
var assert = require('assert');
var swig = require('swig');
// Constructor
var TemplateEngine = function(args) {
assert.ok(args.engine, 'engine is required');
var templateEngine = {};
templateEngine.engine = args.engine;
templateEngine.Render = function(templateString, model) {
var result = swig.render(templateString, model);
return result;
};
return templateEngine;
};
module.exports = TemplateEngine;
and here's an example of the code style I'd like to use, but which produces a "TypeError: Object is not a function" error because I'm not actually assigning to module.exports, just a variable that copied whatever it was pointing to.
(function(templateEngine) {
var assert = require('assert');
var swig = require('swig');
templateEngine = function(args) {
assert.ok(args.engine, 'engine is required');
var templateEngine = {};
templateEngine.engine = args.engine;
templateEngine.Render = function (templateString, model) {
var result = swig.render(templateString, model);
return result;
};
return templateEngine;
};
})(module.exports);
Is there a way for me to use the above self-executing format and have my module export a Constructor?
Share Improve this question asked Feb 13, 2015 at 19:44 Tim HardyTim Hardy 1,7471 gold badge19 silver badges44 bronze badges 4- module.exports needs to be assigned in the 2nd code, maybe something like module.exports=new templateEngine(); ? – dandavis Commented Feb 13, 2015 at 19:46
- 1 you have to many templateEngine variables... – lujcon Commented Feb 13, 2015 at 19:51
- This style is not needed. You can use it to hide private variables... but on client side. With modules in nodejs everything not assigned to module.exports is private so this pattern is useless. – lujcon Commented Feb 13, 2015 at 19:57
- I agree it doesn't provide any functional benefit, but it's pretty nice for organization. You can name module.exports whatever you want in the parm, like "controller" then whatever you assign to controller.someProp = or controller.someFunction = is what you're exporting. I suppose you can do the same thing with an object, so it's more of a preference. I'm realizing all of that is lost when exporting a constructor, so it might be extra useless in this case. – Tim Hardy Commented Feb 13, 2015 at 20:12
1 Answer
Reset to default 9In your second example, you are simply overwriting the templateEngine
parameter, and that's not going to have any effect.
To get the same result as your first example, simply:
Pass module
into your IIFE:
(function(module) {
})(module);
Assign a property to that:
(function(module) {
var assert = require('assert');
var swig = require('swig');
module.exports = function (args) {
...
};
})(module);
本文标签: javascriptNodeJSHow to assign constructor to moduleexports in selfexecuting functionStack Overflow
版权声明:本文标题:javascript - NodeJS - How to assign constructor to module.exports in self-executing function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744094650a2532672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论