代码之家  ›  专栏  ›  技术社区  ›  A. L

javascript-对象引用未按预期进行引用

  •  0
  • A. L  · 技术社区  · 7 年前

    因此,我有以下代码:

    async function runQuery(input)
    {
      return input
    }
    
    async function createAccounts()
    {
      return [1, 2]
    }
    
    
    
    (async function ()
    {
      var
        creator1_ids =
            {},
        creator2_ids =
            {}
    
      let all_accounts =
        await createAccounts(2)
    
      const tables =
        [
          "list",
          "display_list",
          "map"
        ]
    
      for (let index in all_accounts)
      {
        let req =
          {
            session:
            {
              account:
                null
            }
          }
        req.session.account =
          all_accounts[index]
        let the_ids =
            index === 0
                ? creator1_ids
                : creator2_ids
    
        // Create item
        let result =
          await runQuery()
    
        the_ids.item = 
          1
    
        for (let table of tables)
        {
          result =
              await runQuery(table)
          the_ids[table] = 
              result
        }
      }
      
      console.log(creator1_ids)
      console.log(creator2_ids)  
    })()

    现在javascript使用对象作为引用(因此,如果将另一个变量指定给一个对象,则更改任何一个变量都会更改整个对象),但是这里的情况似乎不是这样。

    creator1_ids creator2_ids 已经满了。我在等你 创建者1_id 以同样的方式填写。

    async function runQuery(input)
    {
      return input
    }
    
    async function createAccounts()
    {
      return [1, 2]
    }
    
    
    
    (async function ()
    {
    
        var
          creator1_ids =
              {},
          creator2_ids =
              {}
        let all_accounts =
            await createAccounts(2)
    
        const tables =
            [
                "list",
                "display_list",
                "map"
            ]
    
        async function generate(the_ids, account)
        {
           let req =
            {
              session:
              {
                account:
                  null
              }
            }
    
          // Create item
          let result =
            await runQuery()
    
          the_ids.item = 
            1
    
          for (let table of tables)
          {
            result =
                await runQuery(table)
            the_ids[table] = 
                result + account
          }
        }
    
        await generate(creator1_ids, all_accounts[0])
        await generate(creator2_ids, all_accounts[1])
    
        console.log(creator1_ids)
        console.log(creator2_ids)
    })()
    2 回复  |  直到 7 年前
        1
  •  3
  •   David    7 年前

    结论无效,因为调试有缺陷。更仔细地查看调试器,特别是在这一行:

    let the_ids =
      index === 0 ?
      creator1_ids :
      creator2_ids
    

    index 从不 等于 0 . 信息技术 相同的 '0' . 所以 === 永远不会 true

    更改比较,您将得到预期的结果:

    let the_ids =
      index == 0 ? // compare values regardless of type
      creator1_ids :
      creator2_ids
    

    然后您的循环将首先修改 creator1_ids creator2_ids . 演示:

    async function runQuery(input) {
      return input
    }
    
    async function createAccounts() {
      return [1, 2]
    }
    
    
    
    (async function() {
      var creator1_ids = {},
        creator2_ids = {}
    
      let all_accounts = await createAccounts(2)
    
      const tables = [
        "list",
        "display_list",
        "map"
      ]
    
      for (let index in all_accounts) {
        let req = {
          session: {
            account: null
          }
        }
        req.session.account = all_accounts[index]
        let the_ids =
          index == 0 ?
          creator1_ids :
          creator2_ids
    
        // Create item
        let result = await runQuery()
    
        the_ids.item = 1
    
        for (let table of tables) {
          result = await runQuery(table)
          the_ids[table] = result
        }
      }
    
      console.log(creator1_ids)
      console.log(creator2_ids)
    })()
        2
  •  2
  •   imk    7 年前

    指数 一串 所以使用引号。见下文

    let the_ids = index === '0'? creator1_ids : creator2_ids
    

    而不是

    let the_ids = index === 0  ? creator1_ids: creator2_ids