javascript - Javascript - 是否可以将参数传递给已经有参数的函数?
问题描述
是否可以将参数添加到作为参数传递的函数中?我到达了这个尝试:
function one() {
const args = Array.prototype.slice.call(arguments);
const func = args[0];
const moreArgs = [5]; /// i want number 5 to be arg2
func.apply(this, moreArgs);
}
function two(arg1, arg2) {
console.log(arg1);
console.log(arg2);
}
const call2 = function(){
return two(3);
}
one(call2)
/// 我得到的输出是:
3
undefined
/// 输出目标:
3
5
这种行为或类似的东西可以在javascript中完成吗?
解决方案
使用two(3, ...arguments);
来实现这样的事情:
function one() {
const args = Array.prototype.slice.call(arguments);
const func = args[0];
const moreArgs = [5];
func.apply(this, moreArgs);
}
function two(arg1, arg2) {
console.log(arg1);
console.log(arg2);
}
const call2 = function(){
return two(3, ...arguments); // this line was modified
}
one(call2)
推荐阅读
- javascript - 如何让导航栏下拉选项在没有 JQuery 的情况下工作
- java - 指定用户过滤器时出现 NoClassDefFoundError
- ssis - 在 Visual Studio 2019 中找不到要引用的 DTS 管道组件
- jenkins - svn:external 中带有 jenkinsfile 的多分支管道
- html - HTML/CSS 如何删除按钮图标
- python - 如何解析这个文本文件?
- python - 如何使用 ElementTree 从深度嵌套的 XML 子元素中提取属性?
- javascript - 偏移量返回上一个位置(在排序之前) - JQuery UI
- r - 基于 r 中单独列中的单个输入删除列中的一组输入
- woocommerce - WooCommerce 产品导入元数据包