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

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

    You can use typeof to see what's happening here:

    >>> typeof(['one', 'two', 'three'])
    "object"
    >>> typeof(['one', 'two', 'three'].join)
    "function"
    >>> typeof(arguments)
    "object"
    >>> typeof(arguments.join)
    "undefined"
    

    Here you can see that typeof returns "object" in both cases but only one of the objects has a join function defined.

提交回复
热议问题