[英]How optional arguments 'fall through' functions in Javascript
编辑:我想更好地了解内部函数如何“接受”传递给函数的其他参数。 (例如,该函数需要一个参数,而我们给它三个参数。另外两个参数又去哪里了?)。 为了这个问题,我想避免使用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)
我认为您在理解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
我认为您正在寻找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)
通过依次检查父作用域中的变量来解析变量,直到达到全局作用域为止。
在您的第二个示例中,当从inside2
inside
调用inside2
,虽然x
变量的值未定义,但已经定义了。 由于运行时凸轮在inside
函数范围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
}
您的外部函数正在返回一个函数。 这两个函数都是相同的,因此它们都返回相同的东西就不足为奇了。 我认为您期望内部返回x值作为闭包的一部分。 问题是您已使用相同的变量名称定义了一个函数级变量,从而使其有效地覆盖了闭包值。 将内部的变量名称更改为其他名称,我想您会看到您所期望的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.