Prabhat Mishra
Prabhat Mishra

Reputation: 1021

access functions of self invoking function in javascript with an object

I have a same structure of a piece of code and want to access functions defined in other files ...how can i do that tried module.exports & require

app.js

var x= require('./invkFunc1');

x.display1();
x.display2();
x.display3(); 

invkFunc1.js

var x = require('./invkFunc2');

(function(x){
    //var x=  {}
    disp1 = "hello i am from display1"
    x.display1 = function(){
        console.log(disp1)
    }
    return x;
}(x));

invkFunc2.js

var x = require('./invkFunc3');

(function(x){
    //var x=  {}
    x.disp2 = "hello i am from display2"
    x.display2 = function(){
        console.log(x.disp2)
    }
   return x;
}(x));

invkFunc3.js

module.exports = 

(function(){

var x=  {}
    x.disp3 = "hello i am from display3";
    x.display3 = function(){

        console.log(x.disp3)
    }
   return x;
}());

error:

x.display1();
  ^

TypeError: x.display1 is not a function
    at Object.<anonymous> (c:\Users\prabhat.mishra\Desktop\Chrome-DOM-EXT\extension as on 2rd july 2018\SampleExtension\module.exports\app.js:39:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

from all these example i want to see, how can i access the lower level files and make the functionsavailable which is defined in some file to all the files present.
doing this so that i can achieve modularization as before by placing these codes in one file was working perfectly.

Working code: : trying to achieve modularization from above code of below code

var x ={};



(function(){

  //  var x=  {}
        x.disp3 = "hello i am from display3";
        x.display3 = function(){

            console.log(x.disp3)
        }
    }());

    (function(){
      //  var x=  {}
        x.disp2 = "hello i am from display2"
        x.display2 = function(){
            console.log(x.disp2)
        }
        console.log("call in invkFunc2");
       //x.display3();
    }());

(function(){
    //var x=  {}
    x.disp1 = "hello i am from display1"
    x.display1 = function(){
        console.log(x.disp1)
    }
}());

x.display1()
x.display2();
x.display3();

Thanks in advance!

Upvotes: 1

Views: 375

Answers (1)

oliver34
oliver34

Reputation: 330

if you want use "require",you should exports first,you can code like this

app.js:

var x = require('./invkFunc1');

x.display1();
x.display2();
x.display3();

invkFunc1.js

var x = require('./invkFunc2');
var disp1 = "hello i am from display1"
x.display1 = function(){
  console.log(disp1)
}
module.exports = x;

invkFunc2.js

var x = require('./invkFunc3');
var disp2 = "hello i am from display2"
x.display2 = function(){
  console.log(disp1)
}
module.exports = x;

invkFunc3.js

var x = {}
x.disp3 = "hello i am from display3";
x.display3 = function () {
  console.log(x.disp3)
}
module.exports = x;

Upvotes: 1

Related Questions