Reputation: 16629
I am trying to create a closure
function init(x){
var y = 10;
return function display(a){
return y + a + x;
}
display(5);
}
init(4);
Above closure should return 19.
However, it returns a function.
Upvotes: 0
Views: 29
Reputation: 9576
The last line in your init function is unreachable, once it's after a return statement. If you want to return a primitive value, you have to return the result of the display function.
function init(x){
var y = 10;
function display(a){
return y + a + x;
}
return display(5);
}
init(4);
Upvotes: 0
Reputation: 36
You have to move the "return". The return keyword will end the code execution of the function and return the value that's passed to the return (in your case a function). If your result has to be 19, consider using the following code:
function init(x) {
var y = 10;
function display(a) {
return y + a + x;
}
return display(5);
}
init(4);
Upvotes: 0
Reputation: 397
The return is in front of the function, not the call.
function init(x){
var y = 10;
function display(a){
return y + a + x;
}
return display(5);
}
init(4);
Upvotes: 2