About the Javascript Function

Asked 2 years ago, Updated 2 years ago, 17 views

I am a beginner in JavaScript.
If there is a code like the one below, I would like to know why the value of vard is "a function".

functionouter(X,Y){
    vara = 1;
    var inner=function(b){
        varc = X + Y;
        return c+1;
    }
    return inner;
}
vard=outer(2,3);

javascript

2022-09-29 22:03

1 Answers

If you follow the behavior of the program, you can only say that the variable inner is substituted for the variable return, and the variable d is substituted for the return value of outer.To give you more clear advice, you'll be asked how the questioner is misunderstood, where he's stuck, and where he thinks he's wrong right now (it's easier to write answers if the questionnaire says something about him).

One common misconception is that the function f itself is not distinguished from the syntax f(42) of the function call.

var inner=function(b){
    varc = X + Y;
    return c+1;
};

In this part, inner is substituted for the unnamed function itself, not the result of calling the function.If you want to substitute the result of the call, for example:

 var inner=(function(b){
    varc = X + Y;
    return c+1;
})(42);

Also, in this section:

vard=outer(2,3);

This line gives arguments to the function outer, not to the returned function value.If you want to give an argument to the returned function value, for example:

vard=(outer(2,3))(42);


2022-09-29 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.