0

I have read a lot about passing an array as an function parameter for different return values. But it doesn't work if I have three js file.

main.js

module.exports = function(){
      this.deliver = require('./file1.js'); 
      this.start = require('./file2.js');  
}

file1.js

module.exports = function(id){
  require('./main.js')();

  function deliver(id){
  console.log('Does deliver() work?:'+id);
    var arr = new Array(2);
    arr[0] = 'content 1';
    arr[1] = 'content 2';
    return arr;
  }
}

file2.js

require('./main.js')();

start.apply(null, deliver('yes'));

function start(x, y){
  console.log('x: '+x);
  console.log('y: '+y);
}

Do you have an idea, why deliver is not defined?

@Jose, thanks. Yes, that's the whole code. If everything is in one file does it work. But not in three files...

@Quentin, thanks. Do you have an idea how I could make it work with three file? --> I try to outsource a task from file2 to file1 because I will need it for other tasks too. At the end I need the result of file1 back in file2 to work with it. In my example I have omitted the outsourcing.

Thanks for your hint, Qentin. I have updated the code above. deliver(x) contains now a parameter, that I would like to get back into file2.js. It already arrives file1 but doesn't come back.

I'm still trying to find a solution. I have understood how to pass data from file2 to file1 (or vice versa).

main.js

module.exports = function(){
      this.file1 = require('./file1.js'); 
      this.file2 = require('./file2.js');  
}

file1.js

require('./main.js')();

//file2.receive('yes', 'content 1');


module.exports = {

  deliver : function(id){
  console.log('Does deliver() work?:'+id);
    return file2.receive(id, 'content 1');
  }
}

file2.js

require('./main.js')();

file1.deliver('yes');


module.exports = {

  receive : function(x, y){
    console.log('x: '+x);
    console.log('y: '+y);
  }
}

I can start file2 and pass data to file1 if "return file2.receive(id, 'content 1');" is faded out. I also can start with file1 and pass data to file2 as long as "file1.deliver('yes');" is faded out. Why don't work both at the same time?

3
  • 1
    Is that the whole code for file2.js? Commented Jun 10, 2016 at 14:55
  • "BTW, the following doesn't work too" — Of course it doesn't. deliver is still out of scope. Commented Jun 10, 2016 at 15:05
  • Yes, my fault. My coding skill are slow, even in understanding your points. Commented Jun 10, 2016 at 15:09

1 Answer 1

3

Variables in modules are locally scoped, and function declarations declared inside other functions are scoped to those functions.

this.file1 is assigned the exported function from file1.js. The deliver function is only accessible inside that function.

What's more, file2.js has no access to anything in file1.js because

  • neither of them require the other and
  • nothing from either of them is passed (as an argument) to the other by anything which does have access to both of them

The contents of file2.js have no access to deliver because it is in a completely different scope.

You'd need a major revision of the code to make it accessible. I'm not even sure where to begin with that because your code is so abstract that it impossible to tell what it is trying to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.