JavaScript MemberExpression AST 节点中的计算和可选意味着什么?

问题描述 投票:0回答:2

我有

a.x = b.y
作为 AST 节点:

{
  "type": "ExpressionStatement",
  "start": 122,
  "end": 131,
  "expression": {
    "type": "AssignmentExpression",
    "start": 122,
    "end": 131,
    "operator": "=",
    "left": {
      "type": "MemberExpression",
      "start": 122,
      "end": 125,
      "object": {
        "type": "Identifier",
        "start": 122,
        "end": 123,
        "name": "a"
      },
      "property": {
        "type": "Identifier",
        "start": 124,
        "end": 125,
        "name": "x"
      },
      "computed": false,
      "optional": false
    },
    "right": {
      "type": "MemberExpression",
      "start": 128,
      "end": 131,
      "object": {
        "type": "Identifier",
        "start": 128,
        "end": 129,
        "name": "b"
      },
      "property": {
        "type": "Identifier",
        "start": 130,
        "end": 131,
        "name": "y"
      },
      "computed": false,
      "optional": false
    }
  }
}

computed
optional
是什么意思?有哪些例子?

javascript abstract-syntax-tree
2个回答
1
投票

这就是

a.b[c.d[e].f].g
的样子:

{
  "type": "Program",
  "body": [
    {
      "type": "MemberExpression",
      "object": {
        "type": "MemberExpression",
        "object": {
          "type": "MemberExpression",
          "object": {
            "type": "Identifier",
            "start": 1,
            "end": 2,
            "name": "a"
          },
          "property": {
            "type": "Identifier",
            "start": 3,
            "end": 4,
            "name": "b"
          },
          "computed": false
        },
        "property": {
          "type": "MemberExpression",
          "object": {
            "type": "MemberExpression",
            "object": {
              "type": "MemberExpression",
              "object": {
                "type": "Identifier",
                "start": 5,
                "end": 6,
                "name": "c"
              },
              "property": {
                "type": "Identifier",
                "start": 7,
                "end": 8,
                "name": "d"
              },
              "computed": false
            },
            "property": {
              "type": "Identifier",
              "start": 9,
              "end": 10,
              "name": "e"
            },
            "computed": true
          },
          "property": {
            "type": "Identifier",
            "start": 12,
            "end": 13,
            "name": "f"
          },
          "computed": false
        },
        "computed": true
      },
      "property": {
        "type": "Identifier",
        "start": 15,
        "end": 16,
        "name": "g"
      },
      "computed": false
    }
  ]
}

computed
代表
object[property]


0
投票

可选样品:

obj?.myProperty

计算样本:

obj[expr]
© www.soinside.com 2019 - 2024. All rights reserved.