Search code examples
arraysactionscript-3future-proofstringification

Is Array.toString() guaranteed to remain as is in ActionScript 3?


Is it fine to display the output of Array.toString() to the user, or is there a possibility that the string format could change in future versions of ActionScript 3 or other compilers?


Solution

  • Here's an excerpt describing Array.toString from ECMA-262, which ActionScript 3 follows very closely:

    15.4.4.2

    Array.prototype.toString ( ) When the toString method is called, the following steps are taken:
    1. Let array be the result of calling ToObject on the this value.
    2. Let func be the result of calling the [[Get]] internal method of array with argument "join".
    3. If IsCallable(func) is false, then let func be the standard built-in method Object.prototype.toString (15.2.4.2).
    4. Return the result of calling the [[Call]] internal method of func providing array as the this value and an empty arguments list.

    And Array.join:

    15.4.4.5

    Array.prototype.join (separator) The elements of the array are converted to Strings, and these Strings 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:
    1. Let O be the result of calling ToObject passing the this value as the argument.
    2. Let lenVal be the result of calling the [[Get]] internal method of O with argument "length".
    3. Let len be ToUint32(lenVal).
    4. If separator is undefined, let separator be the single-character String ",".
    5. Let sep be ToString(separator).
    6. If len is zero, return the empty String.
    7. Let element0 be the result of calling the [[Get]] internal method of O with argument "0".
    8. If element0 is undefined or null, let R be the empty String; otherwise, Let R be ToString(element0).
    9. Let k be 1.
    10. Repeat, while k < len
    a. Let S be the String value produced by concatenating R and sep.
    b. Let element be the result of calling the [[Get]] internal method of O with argument ToString(k).
    c. If element is undefined or null, Let next be the empty String; otherwise, let next be ToString(element).
    d. Let R be a String value produced by concatenating S and next.
    e. Increase k by 1.
    11. Return R.

    So, the default behavior is very well defined, and won't change.