gpt4 book ai didi

javascript - 将一个函数传递给一个数组,然后调用它

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:29 26 4
gpt4 key购买 nike

我正在尝试将一个函数添加到数组中,然后稍后再调用它。

// So add it here
var list = [{ Name: "Name1", Name2: "Name", Function: test() }]

// Then call it later which will run the function
list.Function;

function test(){
var test = "test";
console.log(test);
}

最佳答案

将函数放入列表时,您可能不想调用该函数,而将 test() 添加到数组时,您就是这样做的。这将在您创建列表并将结果添加到列表时调用该函数。此外,要在引用它时调用它,您需要包含索引:

function test(){
var test = "test";
console.log(test);
}
// So add it here
var list = [{ Name: "Name1", Name2: "Name", Function: test }] // just add a reference to test no `()`

// Then call it later which will run the function
list[0].Function(); // list is an array so you need to reference the first item

关于javascript - 将一个函数传递给一个数组,然后调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541298/

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