admin管理员组文章数量:1289901
{This has nothing to do with Twitter}
Bit of an interesting question, interesting in that its probably stupid and you can laugh but I will have at least an answer to this damn itch.
Currently I use
var Bootstrap = require('library/Bootstrap');
Bootstrap.run();
When what would be really great is if I could do something like this in the Bootstrap index.js
module.exports.Bootstrap = My_Bootstrap;
And call it willy nilly like this
require('library/Bootstrap');
Bootstrap.run();
Without having to declare another variable to my space, is there a way to do this or am I staring at a screen wondering, dreaming, getting lost, ing back and wasting time?
edit So this is what I did in the end:
I created a single global object and am only adding important modules to it so they can be accessed and instantiated. This turned out to be an amazing answer and solution and really handy
{This has nothing to do with Twitter}
Bit of an interesting question, interesting in that its probably stupid and you can laugh but I will have at least an answer to this damn itch.
Currently I use
var Bootstrap = require('library/Bootstrap');
Bootstrap.run();
When what would be really great is if I could do something like this in the Bootstrap index.js
module.exports.Bootstrap = My_Bootstrap;
And call it willy nilly like this
require('library/Bootstrap');
Bootstrap.run();
Without having to declare another variable to my space, is there a way to do this or am I staring at a screen wondering, dreaming, getting lost, ing back and wasting time?
edit So this is what I did in the end:
I created a single global object and am only adding important modules to it so they can be accessed and instantiated. This turned out to be an amazing answer and solution and really handy
Share Improve this question edited Jul 25, 2012 at 9:30 Dave Mackintosh asked Jul 16, 2012 at 14:59 Dave MackintoshDave Mackintosh 2,7962 gold badges32 silver badges41 bronze badges3 Answers
Reset to default 7Well you can just modify the global
object. This is similar to window object on browsers. So you can write a wrapper for your module like the following:
global.Bootstrap = My_Bootstrap
The global object is shared between modules. Then you can just use with Bootstrap
wherever you are.
Nope, you have to declare the variable. You could get inside the require
function, though, and make that do the work for you. I'm not overly familiar with Bootstrap's internal architecture but it seems like something like this should do the trick:
// put this in some out-of-the-way cubbyhole somewhere
var oldRequire = require;
function require(path) {
Bootstrap = oldRequire(path);
}
// and this would go in your main file
require("library/Bootstrap");
Bootstrap.run();
You cannot do it the way you say, that implies a global, which is almost certainly not what you want.
However what yo can do is this:
require('your_library').call(this,arg1,arg2);
For instance you could be using the node-validator library.
Your are intended to use it like:
sanitize = require('validator').sanitize
var str = sanitize('aaaaaaaaab').ltrim('a');
But you can skip all that and do
var str = require('validator').sanitize('aaaaaaaaab').ltrim('a')
As far as I understand there are no downsides to doing this either in terms of memory/speed.
In your specific case, you are aware you can skip a bit...
File: MyBootStrap.js
Module.exports = MyBootstrap
File: index.js (or other)
require('library/Bootstrap.js')()
本文标签: javascriptNode JS require without varStack Overflow
版权声明:本文标题:javascript - Node JS require without var - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741399457a2376577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论