-4

Could someone please explain what is wrong with this code and how to fix it? I'm truly lost. Thanks!

var messageProcessing = function() {
    console.log(message);
};

process.on('message', function(message) { 
    messageProcessing();
});
Emilio
  • 1,314
  • 2
  • 15
  • 39
  • 1
    Not really a closure issue. The `messageProcessing()` function tries to use a variable `message` which is not in scope because it is only defined as an argument of the later function expression. – nnnnnn Sep 27 '17 at 03:06
  • 1
    you should specify the problem, what do you get? error? warning? what happens – Hatik Sep 27 '17 at 03:06
  • @nnnnnn But the first one is just a function expression (it's not invoking the function) – Emilio Sep 27 '17 at 03:08
  • @Hatik "ReferenceError: message is not defined" – Emilio Sep 27 '17 at 03:09
  • 2
    *"(it's not invoking the function)"* - The later function expression in `process.on(...)` calls `messageProcessing()` (in response to the message event). And then you get the problem I already mentioned, which is that `message` is not in scope in that function, it's only defined as an argument of the other one. – nnnnnn Sep 27 '17 at 03:10
  • 1
    @Emilio well there should be a `message` for you to use inside `console,log()` – Ramanlfc Sep 27 '17 at 03:10
  • @nnnnnn Right, but "message" exists when it is invoked (inside the second part) – Emilio Sep 27 '17 at 03:11
  • 1
    That's a different scope. Functions don't have access to arguments of other functions like that (only to the arguments of their containing function if you have nested functions, which you don't in this case). – nnnnnn Sep 27 '17 at 03:12
  • @Ramanlfc What do you mean? `message` is part of the callback function of the event – Emilio Sep 27 '17 at 03:12
  • @Emilio `message` is a local variable for the callback – Ramanlfc Sep 27 '17 at 03:13
  • Ok, so how do I fix this? – Emilio Sep 27 '17 at 03:13
  • You need to go learn basic coding. `message is part of the callback function of the event`. That's right, then why you can use `message` at `console.log` ? It does not pass along automatically man – Thaina Yu Sep 27 '17 at 03:14
  • 2
    @Emilio pass `message` to `messageProcessing()` – Ramanlfc Sep 27 '17 at 03:14
  • 2
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/q/500431/218196). JavaScript has **lexical** scope, not dynamic scope. – Felix Kling Sep 27 '17 at 03:23
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) –  Oct 09 '17 at 19:05

2 Answers2

3

Your messageProcessing() function tries to reference a message variable that is out of scope, hence you get a reference error. It can't reference arguments or variables declared inside other functions (unless it is nested inside those other functions, which in this case it isn't, nor would it make sense to nest here).

Note that this isn't really about closures, it is just a basic scope issue.

You can explicitly pass the value through like this:

var messageProcessing = function(message) {  // add argument to this function
    console.log(message);
};

process.on('message', function(message) { 
    messageProcessing(message);             // pass value through
});

...Or you can avoid the intermediate anonymous function entirely:

var messageProcessing = function(message) { // add argument to this function
    console.log(message);
};

process.on('message', messageProcessing);   // note no () after messageProcessing
                                            // because this line doesn't invoke it
                                            // it just passes a reference to `.on()`

...Or you can avoid the named function and do everything you need in the anonymous function:

process.on('message', function(message) {
    console.log(message);
});
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

Two other solutions

Pass the message on to the messageProcessing function

var messageProcessing = function(message) {
    console.log(message);
};

process.on('message', function(message) { 
    messageProcessing(message);
});

or, have messageProcessing function inside the function expression that gets the message

process.on('message', function(message) { 
    var messageProcessing = function() {
        console.log(message);
    };
    messageProcessing();
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87