If I define something in a passed function, e.g.
var name = "Whatever";
Is this now accessible from other parts of the application, or is it limited? What’s the logic behind defining scope in node.js javascript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Any variable defined in a method is only available in that method, if you use
var. If you domyVar = 1;myVar will be global.
Node is like other javascript. Node does define a
globalvariable, so you can do things likeglobal.SOME_CONSTANT = 'A Constant'and then use that like
SOME_CONSTANTanywhere in your code/modules.
Because node is asynchronous, you end up defining a lot of callbacks. i.e. you do a lot of
where the function you passed in gets invoked by
someMethodwhen it is done. Let’s say you invokedsomeModule.someMethodin a function. If you defined a variable in that outer function, so your code looks likethe variable defined in the outer scope is available in the callback because of closures. I suggest you spend some time reading about closures in javascript.