【发布时间】:2021-05-04 10:38:04
【问题描述】:
我需要将运行时参数传递给函数。我正在使用bind 将函数绑定到参数。但是,当调用函数时,运行时参数不可用。设置类似于以下:
const testFunction = (event, message) => {
console.log(message); //message is undefined
}
const bindTheMessage = () => {
testFunction.bind(document.getElementById('testButton'), 'hello');
}
<html>
<body>
<button id='bindButton' onclick='bindTheMessage()'>bind a message</button>
<button id='testButton' onclick='testFunction()'>click to get message</button>
</body>
<html>
有人可以帮忙吗?
【问题讨论】:
-
bind不会改变函数对象,它会返回一个新函数。 -
你的论点的顺序似乎错误 - 你想将
'hello'绑定为message吗?
标签: javascript apply call bind