Javascript 在这个嵌套函数示例中如何传递值
从这里学习Javascript- 我理解这些观点Javascript 在这个嵌套函数示例中如何传递值,javascript,Javascript,从这里学习Javascript- 我理解这些观点 可以在函数中嵌套函数 只能从外部函数中的语句访问内部函数 功能。[嵌套(内部)函数是其包含函数的私有函数 (外部)功能。它还形成一个闭包] 内部函数形成一个闭包:内部函数可以使用 外部函数的参数和变量,而外部 函数不能使用内部函数的参数和变量 功能 然而,我无法理解第二个值是如何一直传递到内部函数的,就好像它是作为参数传递给内部函数的一样 function outside(y) { console.log('y is ' + y);
function outside(y) {
console.log('y is ' + y);
function inside(x) {
console.log('x is ' + x);
return x+y;
}
return inside;
}
var a = outside(3);
//console.log(a); // this is easy to understand
/* output:
y is 3
ƒ inside(x) {
console.log('x is ' + x);
return x+y;
}
*/
var b = a(2); // Not able to clearly understand how 2 is interpreted as parameter to nested function.
// Is this bcoz a function was returned earlier and now we passed 2 to that function??
console.log(b);
/* output
y is 3
x is 2
5
*/
console.log('--------------');
var c = outside(3)(2); // How is 3 passed as outer function paramter and 2 passed as inner function parameter?
console.log('---------');
console.log(c);
/* output
y is 3
x is 2
5
--------------
y is 3
x is 2
---------
5
*/
编辑-1:
非常感谢所有帮助过我们的人,他们理解了这些概念,并且写了这篇文章,正如预期的那样
function outside(y) {
console.log('y is ' + y);
function inside(x) {
console.log('x is ' + x);
function innermost(z) {
console.log('z is ' + z);
return x+y+z;
}
return innermost;
}
return inside;
}
outside(3)(2)(1);
/* output
y is 3
x is 2
z is 1
6
*/
编辑-2:
编写函数的另一种方法,以满足EDIT-1中提到的上述目标
function A(x) {
function B(y) {
function C(z) {
console.log(x + y + z);
}
C(3);
}
B(2);
}
A(1); // logs 6 (1 + 2 + 3)
在本例中,C访问B的y和A的x。这是因为:
- A无法访问C,因为A无法访问任何参数或变量 对于B,其中C是的变量。因此,C只对B是私有的
外部(someValue)
的结果是一个函数。因此,要调用结果(内部)函数,您需要调用两次outside
函数。一次获取内部函数outside(3)
,再次调用outside(3)(4)
Wich与以下内容相同:
outside(3)(4)
更多信息:
outside(3) // this calls the outside function and returns the inside function
outside(3)(4) // this calls the outside function returns the inside
//function as result of first call and calls the inside function with (4)
我想是这样的
function outside(y) {
console.log('y is ' + y);
function inside(x) {
console.log('x is ' + x);
function inide(z) {
console.log('z is ' + z);
return y+z;
}
return x+y, inide;
}
return inside;
}
outside(3)(2)(1);
谢谢,然而,我在这里很难准确地理解这两个是相同的,这是如何变得相同的?这个函数调用语法是什么-外部(3)(4)。来自Java 1.6的背景,很难真正理解这个概念。嗯,多亏了edit+1,所以基本上outside()指的是调用同一个函数两次。。因为调用一次只返回另一个fnYesoutside(3)
结果是一个函数,而您只需再调用一次该函数(4)
@spiderman no它不会调用“同一个函数两次”,它会调用一个函数,然后返回另一个被调用的函数。但第二个调用是内部函数,不一样的功能两次!从参数
到咖喱。。。你学得很快…“这个bcoz函数是之前返回的,现在我们将2传递给了那个函数吗?”——是的,就是这样。您甚至可以尝试多次调用a
。这应该是什么?似乎他的意思是编写两个嵌套函数并调用三次,以实现与我的示例相关的三个数之和<代码>函数外部(y){console.log('y是'+y);函数内部(x){console.log('x是'+x);函数内部(z){console.log('z是'+z);返回x+y+z;}返回内部;}返回内部;}外部(3)(2)(1);/*输出y是3 x是2 z是16*/
请编辑您的答案以添加更多信息。你应该向询问者提供信息,而不是相反!我没问他任何问题,他只是个很酷的蜘蛛侠,我告诉了他一些密码。他是一个蜘蛛侠,所以他很酷,编程也很好。他知道你可以让兄弟冷静下来。谢谢你的建议,愿上帝保佑你的心,愿上帝保佑美国
function outside(y) {
console.log('y is ' + y);
function inside(x) {
console.log('x is ' + x);
function inide(z) {
console.log('z is ' + z);
return y+z;
}
return x+y, inide;
}
return inside;
}
outside(3)(2)(1);