簡體   English   中英

具有兩個括號和兩個參數的JS函數

[英]JS Function With Two Parentheses and Two Params

我試圖理解一個函數如何工作,運行兩個括號和兩個參數。 像這樣:

add(10)(10); // returns 20

我知道怎么寫一個像這樣的兩個參數:

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

add(10,10); // returns 20

我怎么能改變這個功能,以便它可以用一組參數或兩個參數運行,並產生相同的結果?

任何幫助表示贊賞。 直截了當地抓住了我的頭。

提前致謝!

我怎么能改變這個功能,以便它可以用一組參數或兩個參數運行,並產生相同的結果?

幾乎可以這樣做,但我很難想到一個很好的理由。

方法如下:你檢測你的函數接收了多少個參數,如果它只收到一個,你返回一個函數而不是一個數字 - 並且如果它被調用,則將該函數添加到第二個數字中:

 function add(a,b) { if (arguments.length === 1) { return function(b2) { // You could call this arg `b` as well if you like, return a + b2; // it would shadow (hide, supercede) the one above }; } return a + b; } console.log(add(10, 10)); // 20 console.log(add(10)(10)); // 20 

我之前說“ 幾乎 ”因為add函數只接收了一個參數,這並不能保證調用者會調用結果。 他們可以寫:

var x = add(10);

......永遠不要調用x現在引用的函數。

歡迎來到一流功能的精彩世界

在JavaScript中,函數可以返回函數,因為函數只是另一個對象。 一個簡單的實現是這樣的:

function add(x){
    return function addOther(y){
        return x + y;
    };
}

由於閉包和一階函數,這是可能的。

這也讓你做部分應用,像Ramda這樣的庫在很大程度上利用了它。

var addThree = add(3)
addThree(5); // 8

為了延長這兩個是什么TJ克羅德本傑明Gruenbaum說,像圖書館Ramda (披露:我是作者之一),讓你轉換一個簡單的函數是這樣的:

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

通過將其包裝在curry函數的調用中進入討論的樣式:

var add = R.curry(function add(a, b) {
    return a + b;
});

add(3, 5); //=> 8
add(3)(5); //=> 8
var add3 = add(3);
add3(5); //=> 8

我在這個問題上所知道的最好的文章是休·傑克遜的“ 為什么咖喱幫助” 我在Favoring Curry寫了一篇更詳細的文章。


更新

這是curry一個版本比Ramda中的版本簡單一些。 它可以完成上述操作並且相當多一些,但不會做一些Ramda用占位符值做的事情:

// here is a function that takes a function and returns a curried version
// of it, that is, a version that performs the sort of partial application
// you describe.
var curry = function(fn) {
    // first, we detect how many arguments the function has.
    var fnArity = fn.length; 
    var partialApply = function(args) { 
        // now, let's create a function that's curried
        return function () {
            // collect the previous args as the partial, and add the new 
            // ones you just received
            var newArgs = (args || []).concat([].slice.call(arguments, 0));
            // if we have "enough" arguments, we don't need any more partial
            // application and we can call the function.
            if (newArgs.length >= fnArity) {
                return fn.apply(this, newArgs);
            } else { // else we return a partially applied version
                return partialApply(newArgs);
            }
        };
    };

    return partialApply([]); // a function is itself partially applied with 0 args
};
function add() {
  var sum = 0;
  for (var i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  function total() {
    for (var i = 0; i < arguments.length; i++) {
      sum += arguments[i];
    }
    return total;
  }
  total.toString = function () { return sum };

  return total;
}

這適用於任何參數和括號。

https://medium.com/@imdebasispanda/super-function-with-closure-86a58a9a980b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM