如何编写一个可以用两对括号调用的函数

How to write a function that can be called with two pairs of parenthesis?

本文关键字:调用 函数 何编写 一个      更新时间:2023-09-26

最近,我被要求编写add(a)(b)函数,这将返回a+b。我不知道如何用javascript编写这个排序函数如何编写此函数,使其返回a+b

编写一个返回函数的函数:

function add(a) {
    return function(b) {
        return a + b
    }
}

在JavaScript中,函数可以像普通对象一样使用。事实上,它们是对象!

add需要返回一个函数:

function add( a ) {
    return function( b ) {
        return a + b;
    }
}
add(1)(2) --> 3