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

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

在函數(shù)內(nèi)部定義監(jiān)聽函數(shù)

在函數(shù)內(nèi)部定義監(jiān)聽函數(shù)

四季花海 2023-06-29 15:36:40
當我在函數(shù)外部定義函數(shù)時,無法訪問 glide 參數(shù):export const setFocusListenersForKeyboardNavigation = (glide) => {    const slides = glide._c.Html.slides;    for (let i = 0; i < slides.length; i++) {        const currentSlide = slides[i];        const slideButton = currentSlide.querySelector(".js-slide-button");        const slideLink = currentSlide.querySelector(".js-slide-link");        slideButton && slideButton.addEventListener('focus', focusListener);        slideLink && slideLink.addEventListener('focus', focusListener);    }};const focusListener = (event) => {    const activeIndex = glide._i;    const buttonIndex = event.target.dataset.slideIndex;    if (activeIndex !== parseInt(buttonIndex)) {        glide.go(`=${buttonIndex}`);    }};因此,我做了類似的事情:export const setFocusListenersForKeyboardNavigation = (glide) => {    const focusListener = (event) => {        const activeIndex = glide._i;        const buttonIndex = event.target.dataset.slideIndex;        if (activeIndex !== parseInt(buttonIndex)) {            glide.go(`=${buttonIndex}`);        }    };    const slides = glide._c.Html.slides;    for (let i = 0; i < slides.length; i++) {        const currentSlide = slides[i];        const slideButton = currentSlide.querySelector(".js-slide-button");        const slideLink = currentSlide.querySelector(".js-slide-link");        slideButton && slideButton.addEventListener('focus', focusListener);        slideLink && slideLink.addEventListener('focus', focusListener);    }};我想知道這是黑客還是好的做法?有沒有更方便的方法來做到這一點。
查看完整描述

1 回答

?
慕田峪7331174

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

將功能放在外面更好。主要是為了可讀性和測試,但如果您的函數(shù)被調(diào)用很多次(例如數(shù)百次),那么每次重新定義甚至可能會影響性能。


您可以向偵聽器添加箭頭函數(shù),它將使用正確的參數(shù)調(diào)用 focusListener。你可以這樣做:


export const setFocusListenersForKeyboardNavigation = (glide) => {

    const slides = glide._c.Html.slides;

    for (let i = 0; i < slides.length; i++) {

        const currentSlide = slides[i];

        const slideButton = currentSlide.querySelector(".js-slide-button");

        const slideLink = currentSlide.querySelector(".js-slide-link");

        slideButton && slideButton.addEventListener('focus', (event) => {focusListener(event, glide)});

        slideLink && slideLink.addEventListener('focus', (event) => {focusListener(event, glide));

    }

};


const focusListener = (event, glide) => {

    const activeIndex = glide._i;

    const buttonIndex = event.target.dataset.slideIndex;

    if (activeIndex !== parseInt(buttonIndex)) {

        glide.go(`=${buttonIndex}`);

    }

};


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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