Why doesn't .join() work with function arguments?

后端 未结 8 1467
遇见更好的自我
遇见更好的自我 2021-02-01 11:59

Why does this work (returns \"one, two, three\"):

var words = [\'one\', \'two\', \'three\'];
$(\"#main\").append(\'

\' + words.join(\", \") + \'

8条回答
  •  无人共我
    2021-02-01 12:41

    Just use the jQuery utility function makeArray

    arguments is not an Array, it is an object. But, since it so "array-like", you can call the jQuery utility function makeArray to make it work:

    var displayIt = function() {
        return 'the list: ' + $.makeArray(arguments).join(",");
    }   
    $("#main").append('

    ' + displayIt('111', '222', '333') + '

    ');

    Which will output:

    the list: 111,222,333

提交回复
热议问题