简体   繁体   English

如何在javascript中为返回函数的变量赋值

[英]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.

相关问题 如何将javascript事件函数的返回值分配给javascript变量 - how to assign return value of a javascript event function to a javascript variable 如何将javascript函数的返回值分配给python中的变量? - how to assign a javascript function return value to the variable in python? 将函数返回值分配给javascript中的变量 - Assign a function return value to a variable in javascript JavaScript 类型问题:如何将 function 的返回值分配给变量,而不是 function 本身? - JavaScript Type Problem: How do I assign the return value of a function to a variable, but not the function itself? 将函数的返回值赋给变量 - Assign return value of function to variable 单击后如何将函数返回值分配给变量以供以后在javascript中使用? - How to assign the function return value to a variable after a click for later use in javascript? 如何从javascript返回值分配给razor变量? - How to assign value from javascript return to razor variable? 如何将JavaScript函数中的变量值分配给Mason变量? - How to assign the value of the variable in JavaScript function into a Mason variable? 如何将函数分配给javascript变量 - How to assign a function to a javascript variable 返回 JavaScript function 的变量值 - Return variable value of JavaScript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM