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
3 Answers
Reset to default 5See 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 itsthis
value be anArray
object. Therefore, it can be transferred to other kinds of objects for use as a method.
Here's the full algorithm from the spec:
- Let O be ToObject(this value).
- ReturnIfAbrupt(O).
- Let len be ToLength(Get(O,
"length"
)). - ReturnIfAbrupt(len).
- If separator is undefined, let separator be the single-element String
","
. - Let sep be ToString(separator).
- ReturnIfAbrupt(sep).
- If len is zero, return the empty String.
- Let element0 be Get(O,
"0"
). - If element0 is undefined or null, let R be the empty String; otherwise, let R be ToString(element0).
- ReturnIfAbrupt(R).
- Let k be
1
. - Repeat, while k < len
- Let S be the String value produced by concatenating R and sep.
- Let element be Get(O, ToString(k)).
- If element is undefined or null, let next be the empty String; otherwise, let next be ToString(element).
- ReturnIfAbrupt(next).
- Let R be a String value produced by concatenating S and next.
- Increase k by 1.
- 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
版权声明:本文标题:javascript - What does Array.prototype.join.call do in the background for a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741956880a2407040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论