gpt4 book ai didi

javascript - javascript内置函数中的循环检测

转载 作者:可可西里 更新时间:2023-11-01 02:10:02 25 4
gpt4 key购买 nike

我从 Chrome 开发者工具的控制台获得了以下跟踪信息:

> a = [1]
[1]
> b = [2, a]
[2, Array[1]]
> a.push(b)
2
> a.toString()
"1,2,"

toString() 似乎智能地跳过了对象图的递归部分。这是在某处记录的标准行为吗?

最佳答案

[ECMA-262: 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.

NOTE The toString 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. Whether the toString function can be applied successfully to a host object is implementation-dependent.

所有这些基本上意味着结果是对 Array.prototype.join() 的调用,它在 15.4.4.5 中定义并且不强制任何递归检测:

[ECMA-262: 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
    1. Let S be the String value produced by concatenating R and sep.
    2. Let element be the result of calling the [[Get]] internal method of O with argument ToString(k).
    3. If element is undefined or null, Let next be the empty String; otherwise, let next be ToString(element).
    4. Let R be a String value produced by concatenating S and next.
    5. Increase k by 1.
  11. Return R.

The length property of the join method is 1.

NOTE 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. Whether the join function can be applied successfully to a host object is implementation-dependent.

那么,这是标准保证的行为吗? 没有。

关于javascript - javascript内置函数中的循环检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13826068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com