[英]how to assign a value to a variable of a return function in javascript
I am trying to return a function that has an assigned variable in it. 我试图返回一个函数,该函数中具有分配的变量。 looking at the example code below: 查看下面的示例代码:
function a(x){
function b(y){
return x+y;
}
return b;
}
executing a(4) would return the function b in this format: 执行a(4)将以以下格式返回函数b:
function b(y){
return x+y;
}
now how can one make so executing a(4) returns b as below: 现在如何执行a(4)返回b,如下所示:
function b(y){
return 4+y;
}
apologies for the title didn't know quite well how to word the question. 对标题的道歉并不十分清楚该问题的措辞。
Since a(4) is what gives you b, you now need to call b(y). 由于a(4)是为您提供b的原因,因此您现在需要调用b(y)。 So if you wanted x to be 4 and y to be 5, you'd call: 因此,如果您希望x为4,y为5,则可以调用:
a(4)(5) // returns 9
Assign the return value to another variable 将返回值分配给另一个变量
function a(x){ function b(y){ return x+y; } return b; } var add4 = a(4); console.log(add4(10)); var add6 = a(6); console.log(add6(15));
您的代码是正确的,使用方式是a(4)(3)。b中的x是a的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.