第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何理解這個高階函數(shù)閉包發(fā)生了什么

如何理解這個高階函數(shù)閉包發(fā)生了什么

冉冉說 2021-10-07 10:31:52
我知道我下面的代碼是錯誤的,但顯然我遺漏了一些信息。我必須編寫更復(fù)雜的高階函數(shù),但如果我不能真正理解簡單的函數(shù),我就沒有機會。hof.add 應(yīng)該返回傳遞的兩個參數(shù)的總和。很簡單...但鑒于我要創(chuàng)建這個高階函數(shù)必須使用閉包。    hof.add = function(add) {      function makeAdd(a, b) {        return add(a + b);      }      return makeAdd;    };it('returns total of the two arguments', () => {        expect(hof.add(56, 5)).to.be.equal(56 + 5);        expect(hof.add(91, -71)).to.be.equal(91 + -71);      });(給出的框架建立在其之上)hof.add = function() { };
查看完整描述

3 回答

?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

如果您只想使用一個參數(shù)名稱,也許您打算對其進行解構(gòu):function(...add)


const not_hof = {};


// Cheating by using destructuring (...add) 

// and not a HOF since accepts only Numbers as arguments 

not_hof.add = function(...add) {  

  const makeAdd = add.reduce((prev, curr) => {

    return prev + curr;

  }, 0);

  return makeAdd; // and still not a HOF since it returns a Number 

};


console.log(not_hof.add(2, 3));              // 5

console.log(not_hof.add(9, 1, 10));          // 20

console.log(not_hof.add(1, 1, 1, 1, 1, 1));  // 6

PS:上面的函數(shù)也可以表示為:

not_hof.add = (...add) => add.reduce((prev, curr) =>  prev + curr, 0);

不是 HOF(高階函數(shù))

雖然很多人會說上面是一個高階函數(shù)- 因為它返回Array.prototype.reduce,它實際上不是

維基百科 - 高階函數(shù)

在數(shù)學(xué)和計算機科學(xué)中,高階函數(shù)是至少執(zhí)行以下一項操作的函數(shù):

  • 將一個或多個函數(shù)作為參數(shù)(即過程參數(shù)),

  • 返回一個函數(shù)作為其結(jié)果。

但是,add不是一個程序函數(shù)的參數(shù); IE:

過程 P(f):
?返回 f(2,3) * f(9,1)

并且它不返回函數(shù);而一個號碼 返回Array.prototype.reduce

1. HOF - 一個或多個函數(shù)作為參數(shù)

至少傳遞一個函數(shù)作為參數(shù)

const helper = { // Helper functions

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

};



const hof = {};

hof.add = function(fn, add1, add2) { // HOF since it takes a function as argument

  return fn(add1, add2);             // (Returns a Number)

};


// ...But it takes three arguments

console.log(hof.add(helper.add, 56, 5)); // 61

2. HOF - 返回一個函數(shù)

至少返回一個函數(shù):


const hof = {};


hof.add = function(add) { // (Takes a Number argument)

  function makeAdd(b) {

    return add + b;

  }

  return makeAdd;         // HOF since it returns a function

};


// But accepts a single argument at each call

console.log(hof.add(56)(5)); // 61

或喜歡


const hof = {};

hof.add = function(add1, add2) { // NOT LIKE YOUR EXAMPLE, 2 arguments are expected!

  function makeAdd() {

    return add1 + add2;

  }

  return makeAdd; // HOF since it returns a function

};


// ...The function had to be executed ()

console.log(hof.add(56, 5)());     // 61

但在這種情況下,它無法通過您的測試說明:


it('returns total of the two arguments', () => { // nope :( only one argument here...


帶閉合的 HOF

允許使用 a 多次調(diào)用該函數(shù)Function.prototype.toString(),以便在最后一次調(diào)用時返回字符串


const hof = {};


hof.add = function(add) {

  let sum = add;    // Store in local scope

  const makeAdd = (b) => {

    sum += b;       // Access to lexical environment variables

    return makeAdd; // Returns a function (self)  

  }

  makeAdd.toString = () => sum;

  return makeAdd;   // HOF since we return a function

}


const result = hof.add(1)(2)(2)(56);

console.log(result)         // (f) 61

console.log(typeof result); // function

console.log(result == 61)   // true

console.log(result === 61)  // false

// A legitimate test might break since the strict equality operator fails.

讓我們保持簡單。沒有關(guān)閉,沒有 HOF

如果不需要使用數(shù)組解構(gòu)和 Array.prototype.reduce() 的第一個示例,那么只需堅持最簡單的函數(shù)聲明形式:


const not_hof = {

  add: (a, b) => a + b,

  sub: (a, b) => a - b,

  // etc...

};


console.log(  not_hof.add(56, 5)  ); // 61


查看完整回答
反對 回復(fù) 2021-10-07
?
尚方寶劍之說

TA貢獻1788條經(jīng)驗 獲得超4個贊

為什么要在返回的函數(shù)中創(chuàng)建一個函數(shù),而不是最初創(chuàng)建正確的函數(shù)?我會做這樣的事情:


hof.add = function(a, b) {

    return (a + b);

};


查看完整回答
反對 回復(fù) 2021-10-07
?
慕妹3146593

TA貢獻1820條經(jīng)驗 獲得超9個贊

高階函數(shù) (HOF) 只是接受函數(shù)作為參數(shù)或返回另一個函數(shù)的函數(shù)的一個奇特名稱。如果我更了解您,您正在嘗試創(chuàng)建一個可用于添加數(shù)字的加法器函數(shù)。使用您的示例 hof.add 是一個接收函數(shù)的 HOF,并返回另一個可用于將兩個數(shù)字相加的函數(shù)


 hof.add = function(add) {

  function addFunc(a, b) {

    return add(a, b);

  }

  return addFunc;

}


function add(a, b){

  return a + b

}


const addNumbers = hof.add(add);

addNumbers(3, 14)

//17 as answer

addNumbers can recieve any two numbers and it will add them together.

add 是一個將兩個數(shù)字相加的函數(shù),它作為 hof.add 中的參數(shù)接收。hof.add 函數(shù)返回一個名為 addFunc 的函數(shù),該函數(shù)又接收兩個要相加的參數(shù)。干杯


查看完整回答
反對 回復(fù) 2021-10-07
  • 3 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號