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

后端 未结 8 1463
遇见更好的自我
遇见更好的自我 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:30

    If you are not interested on other Array.prototype methods, and you want simply to use join, you can invoke it directly, without converting it to an array:

    var displayIt = function() {
        return 'the list: ' + Array.prototype.join.call(arguments, ',');
    };
    

    Also you might find useful to know that the comma is the default separator, if you don't define a separator, by spec the comma will be used.

提交回复
热议问题