@ajlopez get to know how variable scoping in JavaScript works (what is 
global, how this and how local variable works), and you'll get the answers 
:)


On Saturday, July 7, 2012 2:11:56 PM UTC+2, ajlopez wrote:
>
> Hi people!
>
> A very simple question. When I write in the Node repl:
>
> this.foo = 'bar';
> console.log(foo);
>
> it's ok, foo is defined. But writing that code in file.js and running
>
> node file.js
>
> then foo is not defined.
>
> Or require('file.js'), then foo is not defined.
>
> Any way to define a variable in the "current context"? Apparently, "this" 
> properties and "current context" top variables are the same in REPL, but 
> they are not the same in .js, or inside a required module.
>
> I want to do this in a dynamic way:
>
> name = 'foo';
> // ...
> this[name] = 'bar';
>
> so
>
> foo = 'bar';
>
> is not a solution in my context.
>
> I could use:
>
> global[name] = 'bar';
>
> but I want to expose the new "current context" top variables only in my 
> "current context" without pollute the global environment.
>
> I encountered this problem when coding a simple HTML DSL:
> https://github.com/ajlopez/SimpleTags/blob/master/test/exportsTo.js 
> I want the DSL functions to be accesible as:
>
> require('simpletags').exportsTo(????); // ???? == global? this? Now, I 
> should use global to make it works in any situation
>
> then, use the exported function as functions in current context:
>
> html(body(h1("TheNextBigThing")));
>
> instead
>
> var st = require("simpletags");
>
> st.html(st.body(st.h1("TheNextBigThing")));
>
> Any other way?
>
> TIA
>
> Angel "Java" Lopez
> http://ajlopez.wordpress.com
> @ajlopez
> gh:ajlopez
>

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to