Skip to content

类数组处理 #593

@wjc7jx

Description

@wjc7jx

我使用Chrome打印为

{
    "2": 1,
    "3": 2,
    "length": 4
}

分析:

  1. push 方法的行为:
    Array.prototype.push 方法根据对象的 length 属性决定插入位置。调用 push 时,元素会被添加到索引 length 处,并将 length 加 1。
  2. 第一次 push(1):
    初始 length 为 2,元素 1 被添加到索引 2,覆盖原值 3。
    length 变为 3。
  3. 第二次 push(2):
    当前 length 为 3,元素 2 被添加到索引 3,覆盖原值 4。
    length 变为 4。
  4. 类数组的显示优化:
    对象包含 splice 方法和 length 属性,控制台(如 Chrome)会将其视为类数组,优先显示索引属性,隐藏非数字键(如 push 和 splice 方法)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions