javascript 如何在创建过程中引用相同对象的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6824129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:58:31  来源:igfitidea点击:

How do I reference the same Object's properties during its creation?

javascriptobject

提问by fent

I'm trying to do someting like

我正在尝试做类似的事情

o = {
  a: { foo: 42 },
  b: o.a
}

But that returns an error saying o is not defined. I know I can later do o.b = o.a. But I'm wondering if it's possible to define b while I'm in defining o.

但这会返回一个错误,指出 o 未定义。我知道我以后可以做 ob = oa 但我想知道在定义 o 时是否可以定义 b。

采纳答案by Mike Samuel

It is not possible.

这不可能。

The object is not bound in any scope visible to EcmaScript expressions when the property values are evaluated.

当评估属性值时,该对象未绑定在 EcmaScript 表达式可见的任何范围内。

Section 11.1.5of the EcmaScript language spec explains how the object constructor syntax works.

EcmaScript 语言规范的第11.1.5节解释了对象构造函数语法是如何工作的。

The following describes how the object is created as a side-effect of evaluating the first property key value pair

下面描述了如何创建对象作为评估第一个属性键值对的副作用

The production PropertyNameAndValueList : PropertyAssignmentis evaluated as follows:

  1. Let objbe the result of creating a new object as if by the expression new Object()where Objectis the standard built-in constructor with that name.
  2. Let propIdbe the result of evaluating PropertyAssignment.
  3. Call the [[DefineOwnProperty]] internal method of objwith arguments propId.name, propId.descriptor, and false.
  4. Return obj.

生产PropertyNameAndValueList : PropertyAssignment评估如下:

  1. obj成为创建新对象的结果,就像通过表达式new Object()whereObject是具有该名称的标准内置构造函数一样。
  2. propId是评估PropertyAssignment的结果。
  3. 调用[[DefineOwnProperty]]内部的方法的obj与参数propId.namepropId.descriptorfalse
  4. 返回obj

Note that PropertyAssignmentis evaluated after objis created, but objis never bound to any name accessible to an EcmaScript expression.

需要注意的是PropertyAssignment之后才计算的obj被创建,但OBJ从未绑定到一个ECMAScript表达式访问的任何名称。

Only after all the property values are evaluated is anything assigned to oor any other symbol in your program.

只有在评估了所有属性值之后,才会分配给o程序中的任何符号或任何其他符号。

回答by Matt Ball

As @RobG commented — no, you can't.

正如@RobG 评论的那样——不,你不能。

You can, however, use the thiskeyword inside of functions defined as properties of the object, like so:

但是,您可以this在定义为对象属性的函数内部使用关键字,如下所示:

o = {
  a: { foo: 42 },
  b: function () {
      return this.a;
  }
}

console.log(o.b()); // {foo: 42};

回答by ignarukih

This is just ancient history now, but I just learned about getters and setters, which are perfect for your situation, and I'm sure people looking at this issue could get some value from it..

现在这只是古老的历史,但我刚刚了解了getter 和 setter,它们非常适合您的情况,我相信关注这个问题的人可以从中获得一些价值。

o = {
  a: { foo: 42 },
  get b() {
    return this.a
    }
  }

console.log(o.b) // => { foo: 42 }

回答by Jaime

Yet another way to do it:

另一种方法来做到这一点:

(function() {
    var some = { foo: 42 };
    window.o = {
        a: some,
        b: some
    };
})();

alert(o.b.foo);

回答by Hello71

o = {};
o.a = {foo: 42};
o.b = o.a;