admin管理员组

文章数量:1313756

var a = "foo";
var c = Array.prototype.join.call( a, "-" ); // 'f-o-o'

How does the second line of code work? I don't see any conversion of the string to an array and then converting back again, is this happening in the background? I've encountered this kind of code and it's very weird, an array method accepting a string.

var a = "foo";
var c = Array.prototype.join.call( a, "-" ); // 'f-o-o'

How does the second line of code work? I don't see any conversion of the string to an array and then converting back again, is this happening in the background? I've encountered this kind of code and it's very weird, an array method accepting a string.

Share Improve this question edited May 21, 2022 at 1:08 Tony_Henrich 44.2k80 gold badges252 silver badges390 bronze badges asked Nov 29, 2015 at 10:35 daremkddaremkd 8,4248 gold badges44 silver badges70 bronze badges 1
  • NB: Array methods like splice(),reverse(),sort() etc. which modify the array in place will NOT work for strings because strings are immutable – Danield Commented Nov 29, 2015 at 13:25
Add a ment  | 

3 Answers 3

Reset to default 5

See the specification for Array.prototype.join (below). It doesn't require that the this it's operating on be an array, merely that it have a length and properties with names like 0, 1, and so on. Strings do, and so join can work on a string.

From the spec:

NOTE 2 The join function is intentionally generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other kinds of objects for use as a method.

Here's the full algorithm from the spec:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If separator is undefined, let separator be the single-element String ",".
  6. Let sep be ToString(separator).
  7. ReturnIfAbrupt(sep).
  8. If len is zero, return the empty String.
  9. Let element0 be Get(O, "0").
  10. If element0 is undefined or null, let R be the empty String; otherwise, let R be ToString(element0).
  11. ReturnIfAbrupt(R).
  12. Let k be 1.
  13. Repeat, while k < len
    1. Let S be the String value produced by concatenating R and sep.
    2. Let element be Get(O, ToString(k)).
    3. If element is undefined or null, let next be the empty String; otherwise, let next be ToString(element).
    4. ReturnIfAbrupt(next).
    5. Let R be a String value produced by concatenating S and next.
    6. Increase k by 1.
  14. Return R.

A string is an Array like object, because it has the property length and you can access its elements (chars) using [] as of that you can apply most of the array manipulation operations on it.

The Function.prototype.call() calls the function given function with using the first parameter as this and the flowing one as normal parameters.

As of that Array.prototype.join.call(a, "-") will call the function join on the object a in you case the string.

String is array-like object. An array-like object provides indexed access to elements and the property length. You can read more here

本文标签: javascriptWhat does Arrayprototypejoincall do in the background for a stringStack Overflow