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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用reduce組合無限的函數(shù)調(diào)用鏈

使用reduce組合無限的函數(shù)調(diào)用鏈

富國滬深 2019-03-14 14:15:15
一個(gè)對(duì)象, 對(duì)象中有before與enter方法, 其中 before 可以執(zhí)行異步任務(wù), 完成后執(zhí)行回調(diào)再進(jìn)入enter. 現(xiàn)在可能有若干個(gè)這樣的對(duì)象,要求按順序依次執(zhí)行下來. 示例如下:var a = {    before: function(obj, next) {        console.log("a before!");        setTimeout(function() {            next(obj);        }, 1000);        return false;    },    enter: function(obj) {        console.log("a entered!");    }};var b = {    parent: a,    before: function(obj, next) {        console.log("b before!");        setTimeout(function() {            next(obj);        }, 1000);        return false;    },    enter: function(obj) {        console.log("b entered!");    }};var c = {    parent: b,    before: function(obj, next) {        console.log("c before!");        setTimeout(function() {            next(obj);        }, 1000);        return false;    },    enter: function(obj) {        console.log(" c entered!", obj);    }};// 組裝后的函數(shù)var fnChain = function(obj) {    a.before(obj, function(obj) {        a.enter(obj);        b.before(obj, function(obj) {            b.enter(obj);            c.before(obj, function(obj) {                c.enter(obj);            });        });    });};fnChain({ Status: 1, Data: { name: "Anonymous" } });示例中的 fnChain 是我手寫出來的, 現(xiàn)實(shí)中不能這樣. 我想實(shí)現(xiàn)一個(gè)函數(shù), 可以根據(jù)對(duì)象隊(duì)列, 自動(dòng)生成這個(gè) fnChain比如我已經(jīng)有這么一個(gè)隊(duì)列, 然后傳給那個(gè)函數(shù):var arr = [d, c, b, a];getFnChain(arr); // 得到一個(gè) fnChain怎么實(shí)現(xiàn)這個(gè) getFnChain 呢? 我有一個(gè)初步想法是依靠 reduce, 但不幸的是我試了半天, 頭都暈了, 也沒想出來 ╮(╯Д╰)╭ . 看來對(duì) reduce的理解還差得遠(yuǎn).希望有大神能幫助我. 可以參照示例代碼中的 fnChain , 這就是我最終想獲得的 函數(shù)能順帶講一下原理就更好了 (?_?)
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 360 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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