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
toString
method is called, the following steps are taken:
- Let
array
be the result of callingToObject
on thethis
value.- Let
func
be the result of calling the[[Get]]
internal method ofarray
with argument"join"
.- If
IsCallable(func)
isfalse
, then letfunc
be the standard built-in methodObject.prototype.toString
(15.2.4.2).- Return the result of calling the
[[Call]]
internal method offunc
providingarray
as thethis
value and an empty arguments list.NOTE The
toString
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. Whether thetoString
function 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
String
s, and theseString
s 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
O
be the result of callingToObject
passing thethis
value as the argument.- Let
lenVal
be the result of calling the[[Get]]
internal method ofO
with argument"length"
.- Let
len
beToUint32(lenVal).
- If
separator
isundefined
, letseparator
be the single-characterString
","
.- Let
sep
beToString(separator)
.- If
len
iszero
, return the emptyString
.- Let
element0
be the result of calling the[[Get]]
internal method ofO
with argument"0"
.- If
element0
isundefined
ornull
, letR
be the emptyString
; otherwise, LetR
beToString(element0)
.- Let
k
be 1.- Repeat, while
k
<len
- Let
S
be theString
value produced by concatenatingR
andsep
.- Let
element
be the result of calling the[[Get]]
internal method ofO
with argumentToString(k)
.- If
element
isundefined
ornull
, Letnext
be the emptyString
; otherwise, letnext
beToString(element)
.- Let
R
be aString
value produced by concatenatingS
andnext
.- Increase
k
by 1.- Return
R
.The
length
property of thejoin
method is 1.NOTE 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. Whether thejoin
function 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