[英]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.