[removed] Passing parameters to a callback function

后端 未结 13 2121
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 15:48

I\'m trying to pass some parameter to a function used as callback, how can I do that?

function tryMe (param1, param2) {
    alert (param1 + \" and \" + param         


        
相关标签:
13条回答
  • 2020-11-22 16:37

    Use curried function as in this simple example.

    const BTN = document.querySelector('button')
    const RES = document.querySelector('p')
    
    const changeText = newText => () => {
      RES.textContent = newText
    }
    
    BTN.addEventListener('click', changeText('Clicked!'))
    <button>ClickMe</button>
    <p>Not clicked<p>

    0 讨论(0)
提交回复
热议问题