我可以在JavaScript中的对象声明期间引用其他属性吗? [重复]

时间:2021-07-06 23:34:11

This question already has an answer here:

这个问题在这里已有答案:

I am trying to do something like this:

我想做这样的事情:

var obj = {
    a: 5,
    b: this.a + 1
}

(instead of 5 there is a function which I don't want to execute twice that returns a number)

(而不是5,有一个我不想执行两次返回数字的函数)

I can rewrite it to assign obj.b later from obj.a, but can I do it right away during declaration?

我可以重写它以便稍后从obj.a分配obj.b,但我可以在声明期间立即执行吗?

7 个解决方案

#1


29  

No. this in JavaScript does not work like you think it does. this in this case refers to the global object.

不,这在JavaScript中并不像您认为的那样有效。在这种情况下,这是指全局对象。

There are only 3 cases in which the value this gets set:

只有3种情况可以设置它的值:

The Function Case

功能案例

foo();

Here this will refer to the global object.

这里将引用全局对象。

The Method Case

方法案例

test.foo(); 

In this example this will refer to test.

在这个例子中,这将指的是测试。

The Constructor Case

构造函数案例

new foo(); 

A function call that's preceded by the new keyword acts as a constructor. Inside the function this will refer to a newly created Object.

以new关键字开头的函数调用充当构造函数。在函数内部,这将引用新创建的Object。

Everywhere else, this refers to the global object.

在其他地方,这指的是全局对象。

#2


9  

There are several ways to accomplish this; this is what I would use:

有几种方法可以实现这一目标;这就是我要用的:

function Obj() {
    this.a = 5;
    this.b = this.a + 1;
    // return this; // commented out because this happens automatically
}

var o = new Obj();
o.b; // === 6

#3


5  

This should return the correct values:

这应该返回正确的值:

function () {
   var aVar = 5;
   var bVar = aVar + 1;

return {
    a : aVar,
    b : bVar;  
}
}();

#4


3  

As it turns out you can't reference an object inside another object unless the first one is a function. But you can do it this way.

事实证明,除非第一个对象是函数,否则无法在另一个对象中引用对象。但你可以这样做。

    var obj = {
        a: 5
    }

    obj.b = obj.a + 1; // create field b in runtime and assign it's value

If you console.log(obj) you will have

如果你使用console.log(obj)

   obj = {
        a: 5,
        b: 6
     } 

This way you keep the object literal structure for the remaining part of the code

这样就可以保留代码剩余部分的对象文字结构

#5


2  

No, in your example, the value of this doesn't refer to the object literal.

不,在您的示例中,this的值不是指对象文字。

You'll need to assign a value to b after the object has been created in order to base it on another property in obj.

在创建对象之后,您需要为b赋值,以便将其基于obj中的另一个属性。

#6


1  

No. this will take the same meaning as it would outside the definition.

不,这与定义之外的含义相同。

#7


0  

in chrome debugger

在chrome调试器中

> var o = {a: 5, b: this.a+1}
undefined
> o.b
NaN
> o.a
5

#1


29  

No. this in JavaScript does not work like you think it does. this in this case refers to the global object.

不,这在JavaScript中并不像您认为的那样有效。在这种情况下,这是指全局对象。

There are only 3 cases in which the value this gets set:

只有3种情况可以设置它的值:

The Function Case

功能案例

foo();

Here this will refer to the global object.

这里将引用全局对象。

The Method Case

方法案例

test.foo(); 

In this example this will refer to test.

在这个例子中,这将指的是测试。

The Constructor Case

构造函数案例

new foo(); 

A function call that's preceded by the new keyword acts as a constructor. Inside the function this will refer to a newly created Object.

以new关键字开头的函数调用充当构造函数。在函数内部,这将引用新创建的Object。

Everywhere else, this refers to the global object.

在其他地方,这指的是全局对象。

#2


9  

There are several ways to accomplish this; this is what I would use:

有几种方法可以实现这一目标;这就是我要用的:

function Obj() {
    this.a = 5;
    this.b = this.a + 1;
    // return this; // commented out because this happens automatically
}

var o = new Obj();
o.b; // === 6

#3


5  

This should return the correct values:

这应该返回正确的值:

function () {
   var aVar = 5;
   var bVar = aVar + 1;

return {
    a : aVar,
    b : bVar;  
}
}();

#4


3  

As it turns out you can't reference an object inside another object unless the first one is a function. But you can do it this way.

事实证明,除非第一个对象是函数,否则无法在另一个对象中引用对象。但你可以这样做。

    var obj = {
        a: 5
    }

    obj.b = obj.a + 1; // create field b in runtime and assign it's value

If you console.log(obj) you will have

如果你使用console.log(obj)

   obj = {
        a: 5,
        b: 6
     } 

This way you keep the object literal structure for the remaining part of the code

这样就可以保留代码剩余部分的对象文字结构

#5


2  

No, in your example, the value of this doesn't refer to the object literal.

不,在您的示例中,this的值不是指对象文字。

You'll need to assign a value to b after the object has been created in order to base it on another property in obj.

在创建对象之后,您需要为b赋值,以便将其基于obj中的另一个属性。

#6


1  

No. this will take the same meaning as it would outside the definition.

不,这与定义之外的含义相同。

#7


0  

in chrome debugger

在chrome调试器中

> var o = {a: 5, b: this.a+1}
undefined
> o.b
NaN
> o.a
5