Today we'll do ... well at least these topics: 1) Functions .......................... DONE 2) File Based Modules 3) Internal Modules 4) External Node Modules First of all, what is a module? A module is like a .... header file in C/C++, in Node.js. A module can have multiple functions, states, properties, definations and a lot more... Before we dive into modules, lets do some basics first (this will help us in understanding modules better)... ------------------------------- OBJECTS ---------------------------- An object in Node.js has some "key" and "value" pairs. It is very different from objects in C++/Java (where an object is basically an implementation of class....where we say class is a blueprint of an object). Here it is a bit different because the objects here don't follow all the OOPs properties... Lets do a quick example So, that is how you declare an object. We just declared an object in Node. It is really simple... We c...
// CLOSURE function outerFunction(){ // outer variable var x = 20; function innerFunction(someArgument){ // inner variable var y = 10; console.log('Outer variable is ' + x + ' and argument is ' + someArgument); } outerFunction.innerFunction = innerFunction; } // call the outer function outerFunction.innerFunction(50);
// A function can be defined in 3 ways // First way of defining a function // Function Declaration var mySum = function ( x , y ){ var z = x + y; console . log ( 'Sum of ' + x + ' and ' + y + ' is ' + z); } // Second way of declaring function: This is the arrow function var mySum2 = ( a , b ) => { var c = a + b; console . log ( 'Sum is ' + c); } // Third way of declaring a function function mySum3 ( m , n ) { var o = m + n; console . log ( 'Sum of numbers is ' + o); } // // Function call 1 // mySum(120,240); // // Function call 2 // mySum2(234,16); // // Function call 3 // mySum3(100,200); // EXPORTING MODULES module . exports = { mySum: mySum, mySum2: mySum2, mySum3: mySum3 }
Comments
Post a Comment