On Oct 10, 2015, at 5:08 AM, Ginger wrote:

> Here I have a very newbie question here about how to invoke the functions 
> been defined in external js files.
> 
> Assume here I have downloaded a js file has 100 functions and lots of global 
> variables simply looks like:
> 
> abc.js
> var a = 1;
> var b = 2;
> var c = 3;
> ...
> 
> function abc(x){...}
> function cba(y){...}
> 
> .....
> .....
> 
> 
> So now in my main.js file, I simply want to make a call to functions looks 
> like:
> ....
> 
> var v1 = abc(x);
> var v2 = abc(y);
> 
> ....
> 
> then I got an error about abc is not defined. That makes sense, so I guess I 
> have to include the file or import functions in some way. I have seen some 
> solution about export each function in abc.js, but there just too many of 
> them. Also, if I have exported function abc, do I have to export those 
> variables, a, b, c? I am so confused here. @_@

Unlike in a web browser, every JavaScript file gets its own scope in node. That 
means you cannot access its contents from other files, unless you export them.

If you want to access a, b, c from your own file, you have to export them. More 
likely, if a, b, c are only used by those 100 functions and are not meant to be 
used by your own code, then you don't need to export them.


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/53DE8220-ABAE-48BB-A65B-D012C4E51C5C%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to