簡體   English   中英

使用 Object AND 數組參數調用 Javascript function

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM