How module works in nodejs?

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


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

the answer is


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.


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.





What happened after Employee e = new Employee(“ConstructorParam1”);

Employee e = new Employee(“ConstructorParam1”);

  1. It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including system.object (which defines no instance fields of its own). Every object on the heap requires some additional members – called the type object pointer and the sync block index –  used byte the CLR to manage the object. The bytes for these additional members are added to the size of the object.
  1. It allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these bytes are then set to zero
  1. It initializes the object’s type object pointer and sync block index members.
  1. The type’s instance constructor is called, passing it any arguments (the string “ConstructorParam1” in the preceding example) specified in the call to new. Most compiler automatically emit code in a constructor to call a base class’s constructor. Each constructor is responsible for initializing the instance fields defined by the type whose constructor is being called. Eventually, System.Object’s constructor is called, and this constructor method does nothing but return .

After new has performed all of the these operations, it returns a reference (or pointer) to the newly created object. In the preceding code example, this reference is saved in the variable e, which is of type Employee.