How module works in nodejs?

Let’s create a test.js file and type console.log(arguments), what will return?

node

  1. undefined
  2. something else, but what? 🙂

the answer is

node

it is a array of 5 items?

So what are they?

To answer this question, we need to understand how module works in nodejs. Nodejs wraps the module into the function before compiling the modules and that function looks like below. As you can see, the function has 5 parameters, that explains what we saw in the previous picture.

node2

Hard to believe? Let put a break point at console.log(arguments) and debug the code, then you can see the 5 parameters are in the local scope of the current module. Please note, the first line and third line are comments.

node3.PNG

node4

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s