我可以使用
例如,
bind()
来绑定函数和参数。例如,
outputTwo()
需要两个参数。我使用一个参数来绑定它,然后在调用时只需要传递一个参数即可。看起来像这样:<script>
function outputTwo(param1, param2) {
console.log(param1, param2);
}
var boundOutput = outputTwo.bind(null, "what"); // the first param should be an object, to replace <this> object in function, which not needed in our question
boundOutput("the heck?");
</script>
它将输出什么鬼?
。
问题是:我能否将第二个参数绑定到outputTwo()
,而不绑定第一个参数?
在C ++中,有一种称为占位符的东西
。当我需要绑定param2而不绑定param1时,我需要将占位符
传递给param1,看起来像:
auto boundOutput = std::bind(outputTwo, std::placeholders_1, "the heck?");
boundOutput("what");
它将输出What the heck?
,在javascript中是否有类似的内容?
编辑: 我正在编写一个浏览器扩展,需要绑定的函数是由我编写的,但是由浏览器调用。因此,我无法更改参数的类型。
{first:"one"}
并具有从调用方便自我记录代码的优点。 - Keith