Javascript using variable as array name

后端 未结 5 1118
醉话见心
醉话见心 2020-12-18 15:29

I have several arrays in Javascripts, e.g.

a_array[0] = \"abc\";
b_array[0] = \"bcd\";
c_array[0] = \"cde\";

I have a function which takes the arra

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 16:23

    You can either pass the array itself:

    function perform(array) {
        alert(array[0]);
    }
    perform(a_array);
    

    Or access it over this:

    function perform(array_name) {
        alert(this[array_name][0]);
    }
    perform('a_array');
    

提交回复
热议问题