Javascript中可选参数如何“落后”函数

时间:2013-09-25 15:50:41

标签: javascript arguments

编辑:我想更好地理解内部函数如何“获取”传递给函数的附加参数。 (例如,函数需要一个参数,我们给它三个。其他两个去哪里?)。为了这个问题,我想避免使用arguments对象。

我希望更好地理解可选参数如何“落入”内部函数。我将使用下面的代码作为示例:

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)

无论outside()返回inside2还是inside1,都会返回数字20。有没有办法为其他内部函数使用额外的输入?

例如,类似这样的事情(无效代码):

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

4 个答案:

答案 0 :(得分:3)

我认为你在理解JS中函数,变量和参数如何工作方面存在根本问题。我将解释你的代码,希望能够启发你:

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

如果您希望更好地解释您希望使用代码实现的目标,请执行此操作,我将帮助您完成此操作。与此同时,对MDN的功能和范围进行了一些阅读: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope

编辑:经过一番猜测,我想也许你可能会想做这样的事情:

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

JsBin所以你可以摆弄它:http://jsbin.com/oKAroQI/1/edit

答案 1 :(得分:0)

我认为您正在寻找arguments变量。

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');

修改

很难解释你在这里的确切含义,但我认为以下内容可能是您感兴趣的内容:

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)

答案 2 :(得分:0)

通过在父作用域中连续检查变量来解析变量,直到达到全局作用域。

在第二个示例中,insideinside2内调用x变量已定义,但它的值未定义。由于运行时cam在inside的函数范围内找到变量,因此它不会尝试从outside或全局范围获取值。

您可以重命名外部变量,然后执行以下操作:

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

答案 3 :(得分:0)

你在外面的功能正在返回一个功能。这两个函数都是相同的,所以它们都返回相同的东西就不足为奇了。我认为你期望在内部返回x值作为闭包的一部分。问题是您已经使用相同的变量名定义了一个函数级变量,使其有效地覆盖了闭包值。将内部变量名称的名称更改为其他名称,我认为您将看到您的期望。