在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全  HTML/ 模擬ajax異步請(qǐng)求時(shí)出現(xiàn)問(wèn)題

模擬ajax異步請(qǐng)求時(shí)出現(xiàn)問(wèn)題

let mapping = new Map([
    ["/getData/dataList", getData.getDataList()],
    ["/getData/id", getData.getKnowledgeById],
    ["/getData/search", getData.searchKnowledge]
]);

let ajax = new class {
  constructor(mapping) {
      this.mapping = mapping;
  }

  request(option) {
      return this.mapping.get(option.url);
  }
}(mapping);

option是一個(gè)對(duì)象,調(diào)用方式如下:

ajax.request({
        url: '/getData/id',
        id: 1
});

代碼如上所述,mapping是存的一個(gè)url和函數(shù)的映射表,現(xiàn)在存在一個(gè)問(wèn)題是,當(dāng)我調(diào)用的是getKnowledgeById函數(shù)時(shí),這個(gè)函數(shù)是需要一個(gè)參數(shù)的,request里的return只能return回這個(gè)函數(shù),現(xiàn)在的問(wèn)題是:
如何通過(guò)我現(xiàn)在傳入的option對(duì)象中的參數(shù)自動(dòng)判斷我是傳了id還是input值,分別再應(yīng)用到getKnowledgeById和searchKnowledge這兩個(gè)函數(shù)中,返回執(zhí)行結(jié)果?

困惑已久,求大神解答!

補(bǔ)充最后用的方法:

return this.mapping.get(option.url).call(getData, option.args);

這一行就OK!謝謝大神解答給我啟發(fā)!

回答
編輯回答
司令

根據(jù)url確定是id還是input值

let mapping = new Map([
    ["/getData/dataList", getData.getDataList()],
    ["/getData/id", getData.getKnowledgeById],
    ["/getData/search", getData.searchKnowledge]
]);

let ajax = new class {
  constructor(mapping) {
      this.mapping = mapping;
  }

  request(option) {
      if(option.args){
          let func = this.mapping.get(option.url);
          return func(option.args);
      }else{
        return this.mapping.get(option.url);
      }
  }
}(mapping);
2018年5月25日 10:05