代码之家  ›  专栏  ›  技术社区  ›  LongHike

Javascript Object.create:设置数据时内存会发生什么变化?

  •  1
  • LongHike  · 技术社区  · 5 年前

    假设在Javascript中,正在使用一个方法创建一个新对象,以将数据设置到创建的实例。

    let data = {text: 'a text'};
    
    let newObj = Object.create({
      setData(argData) {
        this.data = argData;
      }
    });
    
    // is data and newObj.data twice in memory now?
    

    如果已经有人问过类似的问题,我提前表示歉意(这是撰写本文时的编辑提示)。我知道,我还没有找到他们。也许他们的表述不同,但我不知道如何用其他方式问这个问题。

    2 回复  |  直到 5 年前
        1
  •  7
  •   T.J. Crowder    5 年前

    我想知道,当数据被添加后,该数据会在内存中存在两次,还是newObj.data和data只是指向内存中的同一地址?

    后者。里面装的是什么 newObj.data data 是一种叫做 对象引用 ,而不是对象本身。对象在内存中的其他位置(仅一次);引用是一个值,告诉JavaScript引擎对象在哪里。

    假设你真的打过电话 newObj.setData(data) . 只是 Object.create 它本身并没有设定目标 数据 土地上的财产 newObj

    让我们用ASCII艺术来表现它。完成此操作后:

    let data = {text: 'a text'};
    
    let newObj = Object.create({
      setData(argData) {
        this.data = argData;
      }
    });
    

    …您的内存中有类似的内容(省略了各种细节):

                                                   +−−−−−−−−−−−−−−−−+
    data:[Ref54611]−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−>|   (object)     |
                                                   +−−−−−−−−−−−−−−−−+
                                                   | [[Prototype]]  |−−+
                                                   | text: 'a text' |  |
                                                   +−−−−−−−−−−−−−−−−+  |
                                                                       |
                         +−−−−−−−−−−−−−−−−+                            +−>(Object.prototype)
    newObj:[Ref74135]−−−>|   (object)     |                            |
                         +−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−+  |
                         | [[Prototype]]  |−−−>|      (object)      |  |
                         | text: 'a text' |    +−−−−−−−−−−−−−−−−−−−−+  |
                         +−−−−−−−−−−−−−−−−+    | [[Prototype]]      |−−+
                                               | setData:[Ref55462] |−−−−>(function)
                                               +−−−−−−−−−−−−−−−−−−−−+
    

    然后:

    newObj.setData(data);
    

    …您有类似于:

                                                +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
                                                |                                                |
                                                \    +−−−−−−−−−−−−−−−−+                          |
    data:[Ref54611]−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+−−>|   (object)     |                          |
                                                     +−−−−−−−−−−−−−−−−+                          |
                                                     | [[Prototype]]  |−−+                       |
                                                     | text: 'a text' |  |                       |
                                                     +−−−−−−−−−−−−−−−−+  |                       |
                                                                         \                       |
                         +−−−−−−−−−−−−−−−−−−+                             +−>(Object.prototype)  |
    newObj:[Ref74135]−−−>|   (object)       |                            /                       |
                         +−−−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−+  |                       |
                         | [[Prototype]]    |−−−>|      (object)      |  |                       |
                         | text: 'a text'   |    +−−−−−−−−−−−−−−−−−−−−+  |                       |
                         | data: [Ref54611] |−−+ | [[Prototype]]      |−−+                       |
                         +−−−−−−−−−−−−−−−−−−+  | | setData:[Ref55462] |−−−−>(function)           |
                                               | +−−−−−−−−−−−−−−−−−−−−+                          |
                                               |                                                 |
                                               +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
    

    请注意,概念值“[Ref54611]”在两种格式中都有 数据 (变量)和 (财产)。您从未在代码中实际看到对象引用的值,但可以将它们视为告诉引擎对象所在位置的数字。

        2
  •  0
  •   silicakes    5 年前

    对于JS,情况就是后者,因为对象总是被引用的。

    您可以在此处找到示例: Javascript by reference vs. by value