0

Edit: I want to better understand how internal functions 'take' the additional arguments passed to a function. (E.g. the function requires one argument and we give it three. Where do the other two go?). For the sake of this question, I want to avoid using the arguments object.

I am hoping to gain a better understanding of how optional arguments 'fall through' to internal functions. I'll use the code below as an example:

function outside() {
   var x = 10;
   function inside(x) {
      return x;
   }
   function inside2(a) {
      return a;
   }
   return inside2; // or inside, doesn't seem to make a difference here
}
outside()(20,5)

Regardless of whether outside() returns inside2 or inside1, the number 20 is always returned. Is there any way to utilize additional inputs for the other internal functions?

For example, something like this (invalid code):

function outside() {
   var x = 10;
   function inside(x) {
      return x;
   }
   function inside2(a) {
      return inside();
   }
   return inside2;
}
outside()(20,5)

4 Answers 4

3

I think you have a fundamental problem in understanding how functions, variables and parameters work in JS. I will explain your code hoping to enlighten you:

function outside() {
   var x = 10; // variable x is a local variable in the scope of the outside function. it is not used anywhere
   function inside(x) { 
      // parameter x hides the variable in the outer scope (outside) with the same name 

      // this function returns the first parameter passed to it. 
      // what the value returned is will be decided when the function is called
      return x;
   }
   function inside2(a) {
      // this function also returns the first parameter passed to it.
      return a;
   }
   return inside2; // or inside, doesn't seem to make a difference here
   // no difference because both functions do the same thing
}
outside()(20,5) // this code is equivalent to the following lines:

var temp = outside();  // temp is the return value of outside - in our case the inside2 function
temp(20,5); // the inside2 function is called with 2 arguments. It returns the first one (20) and ignores the other one

If you wish to explain better what you want to achieve with your code, please do so and i will help you do that. In the meantime, some reading on functions and scopes from MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope

EDIT: After some guesswork, i think perhaps you might want to do something like this:

function outside(x,a) {
  function inside1() {
    return x; // returns the x from the outer scope = the first parameter passed to outside
  }
  function inside2() {
    return a; // returns the a from the outer scope = the second parameter passed to outside
  }
  return inside2;
}

outside(20,5)() // returns 5

A JsBin so you can fiddle with it: http://jsbin.com/oKAroQI/1/edit

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

Comments

0

Variables are resolved by successively checking for the variable in the parent scope, until global scope is reached.

In your second example when inside is called from within inside2 the x variable is already defined, although it's value is undefined. Since the runtime cam find the variable in the function scope of inside it doesn't attempt to get a value from outside or the global scope.

You could rename the outer variable and then do something like this:

default_x = 10;
function inside(x){
    x = x || default_x; // Return argument x if it's value is truthy, otherwise use default
    return x
} 

Comments

0

Your function outside is returning a function. Both of these functions are identical so its not surprising that they both return the same thing. I think you are expecting inside to return the x value as part of being a closure. The problem is you have defined a function level variable with the same variable name making it effectively override the closure value. Change the name of the variable name in inside to some other name and I think you will see what you expect.

Comments

0

I think you're looking for the arguments variable.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments

var someFunc = function(arg1) {
  console.log(arguments[1]); //logs 'second arg'
  console.log(arguments.length); //logs 2
};

someFunc('first arg','second arg');

EDIT

It's hard to decipher exactly what you mean here, but I'm thinking something along the lines of the following may be what you are interested in:

function outside() {
   var x = 10;
   function inside(x) {
      return x;
   }
   function inside2(a) {
      if(arguments.length == 2) {
        //return the 'inside' function with the second parameter
        return inside(arguments[1]);
      } 

      return a;
   }
   return inside2;
}
outside()(20,5)

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.