未加星标

In what scope are module variables stored in node.js?

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

When I do this in my node.js module:

var abc = '123';

Where does it go? And by this I mean: in the browser it goes in window.abc (if not executed in a function or otherwise)

If I execute this:

abc = '123';

Then I can find it in global.abc , but that's not how I want it.

Problem courtesy of: skerit

Solution

Unlike the browser, where variables are by default assigned to the global space (i.e. window), in Node variables are scoped to the module (the file) unless you explicitly assign them to module.exports.

In fact, when you run node myfile.js or require('somefile.js') the code in your file is wrapped as follow:

(function (exports, require, module, __filename, __dirname) { // your code is here });

Solution courtesy of: Hector Correa

Discussion

All the other answers are 100% correct, but I thought I would add an expanded/definitive list of the scopes within a Node.js application in case anybody comes across this via Google while starting off learning Node.js or javascript:

Global Scope

Anything declared without the var keyword in any file will be accessible from anywhere running in the same instance of the Node server:

// foo.js bar = 'baz'; // qux.js console.log(bar); // prints 'baz'

Note that this is widely considered to be a bad idea, because it makes your app strongly 'coupled' meaning that you'd have to open foo.js to work out why bar = 'baz' in qux.js

Module Scope

Anything declared with the var keyword at the top level (not inside a function or object, or any other block) of a node.js file is in module scope , and will be accessible from anywhere within the same file, but will not exist anywhere else:

// foo.js var bar = 'baz'; console.log(bar); // prints 'baz' // qux.js console.log(bar); // prints 'undefined' Function Scope

Anything declared using the var keyword within a function will only be accessible from within that function, and not from anywhere else:

// foo.js function myFunction() { var bar = 'baz'; console.log(bar); // prints 'baz' } function myOtherFunction() { console.log(bar); // prints 'undefined' } // qux.js console.log(bar); // prints 'undefined'

JavaScript is function scoped . Unlike other ( block scoped ) languages, variables declared in a block within a function are accessible from anywhere else in that parent function . For example, this means that if you declare a new variable inside inside a loop, it's accessible outside of that loop as well, as long as you're still inside the parent function:

function myFunction() { while (thing === true) { var bar = 'baz'; thing = false; } console.log(bar); // prints 'baz' } Shadowing

If you 'redeclare' an existing variable, e.g. use the var keyword with a variable name that has been used already, then the value associated with that variable name is overwritten within the scope of the new declaration :

var bar = 'foo'; console.log(bar) // prints 'foo' function myFunction() { var bar = 'baz'; console.log(bar); } myFunction(); // prints 'baz' console.log(bar) // prints 'foo'

Discussion courtesy of: Toadfish

Node has a module scope, so var abc = '123' in a module will create a variable which is scoped to (and therefore, reachable only for code in) that module.

See also http://nodejs.org/api/globals.html#globals_global

Discussion courtesy of: robertklep

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:In what scope are module variables stored in node.js?
本站链接:https://www.codesec.net/view/627808.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(98)