Access JavaScript Object Literal value in same obj

2019-01-08 00:33发布

Possible Duplicate:
Self-references in object literal declarations

Is there any way access the value of a property name in the same object literal? Something like this:

myFunction.init({
    varOne: 'something',
    varTwo: this.varOne + 'something else'
})

1条回答
三岁会撩人
2楼-- · 2019-01-08 01:07

No, there is no way to access the object literal that is currently being defined from within the definition itself.

If you want to set properties based on the values of other properties, then you either need to base them both on some external value (that is not a property itself) or run an initializer function after the object literal is defined that can set some properties based on the values of other properties.

查看更多
登录 后发表回答