gpt4 book ai didi

javascript - 传递带参数的函数

转载 作者:行者123 更新时间:2023-11-30 16:57:50 25 4
gpt4 key购买 nike

我想将一个带有参数的函数传递给另一个函数而不对其求值。

此方法无效:

function first (id) {...}

function second (func) {
func();
}

second(first(id));

这个方法可以,但我不能用它,因为参数的数量并不总是相同的:

function first (id) {...}

function second (func, id) {
func(id);
}

second(first, someId);

最佳答案

您可以使用#bind 进行部分应用:

second( first.bind(null, id) )

关于javascript - 传递带参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com