If you really want access to globals there are a couple ways.  One is to
use `global` ir `this` in any standalone non-strict-mode function.

Also node modules are cached.  So if I require `foo` and then set a
property on that object, anyone else who also requires `foo` will have the
exact same object that I mutated.

//foo.js
// empty file, returns an empty exports object


//a.js
var foo = require('./foo');
foo.aWasHere = true


//b.js
require('./a')
var foo = require('./foo')
console.log(foo.aWasHere)

//main.js
require('./b.js')


On Tue, May 15, 2012 at 11:53 AM, Jeff Barczewski <[email protected]
> wrote:

> Node.js is different than loading in the browser in that you don't work
> with globals like you do in the browser (or at least not as much).
>
> So to accomplish what you are after, one way would be to have the plugins
> return a function so that you can pass in the original which will be
> modified.
>
> For instance you pass in your namespace in the function after you require
> and it would be able to add itself that way.
>
> var obj = { };
> require('foo')(obj); // foo.js can add properties to obj
>
>
> So you just have to have foo.js return a function (which takes as a
> parameter the namespace object you want to modify)
>
> Hope that helps.
>
> Jeff
>
>
>
>
> On Tuesday, 15 May 2012 04:54:27 UTC-5, Romuald Quantin wrote:
>>
>> Hi there,
>>
>> I've been trying to achieve something that should be simple but
>> because I'm not a node.js user (yet), I'm not sure how things should
>> be done.
>>
>> I'm trying to create a namespace that I can load in the browser (AMD
>> and not AMD), and in code. I would have different files that add
>> functionalities to that same namespace.
>>
>> http://stackoverflow.com/**questions/10586873/cross-**
>> platform-javascript-**namespacing<http://stackoverflow.com/questions/10586873/cross-platform-javascript-namespacing>
>>
>> Let me know if the example is not clear, I can probably put some code
>> somewhere.
>>
>> The idea in that post is:
>> - I create one namespace: "myns"
>> - in 1 js file I add a property to it: myns.before
>> - in another js file I add another property to it: myns.hello
>> - in another js file I add another property to it: myns.after
>>
>> And I'd like to be able to log:
>> - myns.before
>> - myns.hello
>> - myns.after
>>
>> In the example in stack overflow, it works:
>> - in the browser
>> - in the browser, loaded as a AMD module
>> - doesn't work in node.js, the 3 properties are separated and kind of
>> in different namespace.
>>
>> Could someone bring me some lights how I can achieve that?
>>
>> Cheers.
>>
>> Romu
>
>  --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to