javascript - 如何将数组和函数导入到函数中

标签 javascript arrays function

  1. 如何将我之前创建的现有函数导入到函数中?

  2. 如何将数组导入到函数中?

function namarray (arr[])
{}

function namfunc (fun())
{}

如何用正确的语法编写它?

最佳答案

这两种情况都是一样的,您将所需的内容作为参数传递给函数。对于函数,您只需传递函数名称,因此不要包含 () - 稍后当您想要调用该函数时会使用它

您当然可以选择将输入保存到函数内的变量

// 1) How to import into function an existing function that I have alreqdy created before ?
// 2) How to import into function an array ?

function testFunction () {
  return 'I am the test function';
}

function questionOne (input) {
  console.log(input());
}

function questionTwo (input) {
  console.log(`I was given the input ${input}, which is an ${typeof input}`);
}

questionOne(testFunction);
questionTwo([1,2,3]);

// In reply to comment
let longArray = [1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,111,222,333];
questionTwo(longArray); 

关于javascript - 如何将数组和函数导入到函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58886109/

相关文章:

javascript - 基于动态标准的高效数组过滤

javascript - 将数组添加到 Javascript 对象

c++ - 模板函数和 Const/NonConst 引用参数

c++ - 如何检测作为参数传递的函数的参数类型?

javascript - 如何在emberjs中获取运行转换的目标

javascript - 使用 loadonce 时的 JQGrid 分组问题

javascript - 将 jQuery 延迟 Promise 转换为原生 JavaScript Promise

javascript - 使用 JQuery 为 <a> 标签添加 onclick 属性

javascript - 使用 jQuery 动态添加/删除搜索过滤器

PHP 文件 - 使用数据库问题中的值填充动态 <select> 标记