鍍金池/ 問答/HTML/ 【基礎(chǔ)疑惑】為什么要在方法的最后加上逗號后再寫上參數(shù)?

【基礎(chǔ)疑惑】為什么要在方法的最后加上逗號后再寫上參數(shù)?

疑問在于最后一行,為啥要加上前面一堆參數(shù)?百度了好久沒找到答案,因?yàn)椴磺宄@種叫什么術(shù)語。
代碼如下:

    for(let i=1;i<61;i++){
        const info_sel = '#ajax_loading_con > li:nth-child(' + i + ')';
        const name_sel = '#ajax_loading_con > li:nth-child(' + i + ') > a.info';
        const icon_sel = '#ajax_loading_con > li:nth-child(' + i + ') > a.info > img';
        const href_sel = '#ajax_loading_con > li:nth-child(' + i + ') > a.info';

        //MapReduce方式
        const games = await page.evaluate((gInfo, gName, gIcon, gHref) => {
            return Array.prototype.slice.apply(document.querySelectorAll(gInfo))
                .map(document => {
                    const gameName = document.querySelector(gName).title;
                    const gameIcon = document.querySelector(gIcon).src;
                    const gameHref = document.querySelector(gHref).href;
                    return {
                        gameName,
                        gameIcon,
                        gameHref,
                    };
                })
        }, info_sel, name_sel, icon_sel, href_sel);
回答
編輯回答
氕氘氚

整理一下你可能就看明白了:


let mapper = (gInfo, gName, gIcon, gHref) => {
    return Array.prototype.slice.apply(document.querySelectorAll(gInfo))
        .map(document => {
            const gameName = document.querySelector(gName).title;
            const gameIcon = document.querySelector(gIcon).src;
            const gameHref = document.querySelector(gHref).href;
            return {
                gameName,
                gameIcon,
                gameHref,
            };
        })
};

const games = await page.evaluate(mapper, info_sel, name_sel, icon_sel, href_sel);

javascript 里支持接收一個函數(shù)作為參數(shù)。

2018年5月13日 13:11