I obtained the following trace from Chrome developer tool's console:
> a = [1]
[1]
> b = [2, a]
[2, Array[1]]
> a.push(b)
2
> a.toString()
"1,2,"
It seems the toString() intelligently skipped the recursive part of the object graph. Is this a standard behavior documented somewhere?
A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.
The For-EndFor Statement Structure Another method of performing looping in your scripts is by using the For-EndFor looping statements. Similar to a While loop, a For loop consists of three parts: the keyword For that starts the loop, the condition being tested, and the EndFor keyword that terminates the loop.
[ECMA-262: 15.4.4.2]:Array.prototype.toString ( )When the
toStringmethod is called, the following steps are taken:
- Let
 arraybe the result of callingToObjecton thethisvalue.- Let
 funcbe the result of calling the[[Get]]internal method ofarraywith argument"join".- If
 IsCallable(func)isfalse, then letfuncbe the standard built-in methodObject.prototype.toString(15.2.4.2).- Return the result of calling the
 [[Call]]internal method offuncprovidingarrayas thethisvalue and an empty arguments list.NOTE The
toStringfunction is intentionally generic; it does not require that itsthisvalue be anArrayobject. Therefore it can be transferred to other kinds of objects for use as a method. Whether thetoStringfunction can be applied successfully to a host object is implementation-dependent.
All of this basically means that the result is a call to Array.prototype.join(), which is defined in 15.4.4.5 and doesn't mandate any recursion detection:
[ECMA-262: 15.4.4.5]:Array.prototype.join (separator)The elements of the array are converted to
Strings, and theseStrings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.The join method takes one argument,
separator, and performs the following steps:
- Let
 Obe the result of callingToObjectpassing thethisvalue as the argument.- Let
 lenValbe the result of calling the[[Get]]internal method ofOwith argument"length".- Let
 lenbeToUint32(lenVal).- If
 separatorisundefined, letseparatorbe the single-characterString",".- Let
 sepbeToString(separator).- If
 leniszero, return the emptyString.- Let
 element0be the result of calling the[[Get]]internal method ofOwith argument"0".- If
 element0isundefinedornull, letRbe the emptyString; otherwise, LetRbeToString(element0).- Let
 kbe 1.- Repeat, while
 k<len
- Let
 Sbe theStringvalue produced by concatenatingRandsep.- Let
 elementbe the result of calling the[[Get]]internal method ofOwith argumentToString(k).- If
 elementisundefinedornull, Letnextbe the emptyString; otherwise, letnextbeToString(element).- Let
 Rbe aStringvalue produced by concatenatingSandnext.- Increase
 kby 1.- Return
 R.The
lengthproperty of thejoinmethod is 1.NOTE The
joinfunction is intentionally generic; it does not require that itsthisvalue be anArrayobject. Therefore, it can be transferred to other kinds of objects for use as a method. Whether thejoinfunction can be applied successfully to a host object is implementation-dependent.
So, is it a standard-guaranteed behaviour? No.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With