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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4618541/
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 14:19:14  来源:igfitidea点击:

Can I reference other properties during object declaration in JavaScript?

javascript

提问by serg

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.blater from obj.a, but can I do it right away during declaration?

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

采纳答案by Ivo Wetzel

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

不,this在 JavaScript 中并不像你想象的那样工作。this在这种情况下是指全局对象。

There are only 3 cases in which the value thisgets set:

只有 3 种情况下this会设置该值:

The Function Case

功能案例

foo();

Here thiswill refer to the globalobject.

这里this将引用全局对象。

The Method Case

方法案例

test.foo(); 

In this example thiswill refer to test.

在这个例子this中将参考test.

The Constructor Case

构造函数案例

new foo(); 

A function call that's preceded by the newkeyword acts as a constructor. Inside the function thiswill refer to a newly created Object.

new关键字开头的函数调用充当构造函数。在函数内部this将引用一个新创建的Object.

Everywhere else, thisrefers to the global object.

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

回答by ken

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

回答by kemiller2002

This should return the correct values:

这应该返回正确的值:

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

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

回答by Herbi Shtini

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

通过这种方式,您可以为代码的其余部分保留对象字面量结构

回答by user113716

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

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

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

您需要b在创建对象后为其分配一个值,以便基于obj.

回答by fuzzyTew

No. thiswill take the same meaning as it would outside the definition.

No.this的含义与定义之外的含义相同。

回答by hvgotcodes

in chrome debugger

在 Chrome 调试器中

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