[英]In Javascript, is there a way to convert an object array to parameters on a function call?
[英]Call Javascript function with Object AND array parameters
我看过很多关于使用 Javascripts apply()
和 ES6 省略号调用的帖子,但它们似乎只处理传递 arrays,仅此而已。 我想传递一个 Object 和一个数组。
function myFunction(obj, arr) {
// Not really doing this but you get the idea
if (arr.includes(obj)) {
;;
}
}
let theArr = ["bob", "mike", "steve"];
let o = "sally";
myFunction(o, theArr); // How can I do something equivalent to this?
谢谢。
apply()
的第二个参数是arguments 的数组,如果你想传递一个 object 和一个数组,你只需要将它们包含在数组中。 例如:
function test(el1, el2){ alert(el1.a); alert(el2); alert(this.b); } test.apply( {b: 4}, //First argument, the new *this* variable [{a:1}, [2, 3]] //Second argument is the list of arguments passed to test() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.