Thursday, 5 January 2017

exporting functions from one module to another modul


suppose  we want to export  a function from one .js file to another js file .


/** * Created by deepak gautam on 05-01-2017. */

suppose this is a code in movies.js 
here we are exporting 2 functions 
1->sar 
2->sal 
module.exports= {
    sar :function   ()
    {
        console.log("best movies of sarukh is  chennai express, fans, rawan");

    },

    sal : function  () {
        console.log("best movies of salman  is  munna bhai mbbs  , bajranga bhaijan, ....");

    }
};



// this is the code in app.js file where we are importing the exported  functions in movies.js
var user=require('./movies');// since both modules are  in the same folder so we are using this path
user.sar();



problems with exported  variables ..

suppose we export one variable say x=10,
now two mudules import this variable, 
than if one module change that variable than other module will get the updated value ..


for fixing that we have to do it in a different way
always return a new  created variable 




No comments:

Post a Comment