在同一对象中访问JavaScript Object Literal值 [英] Access JavaScript Object Literal value in same object

查看:108
本文介绍了在同一对象中访问JavaScript Object Literal值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

对象文字声明中的自引用

是有没有办法在同一个对象文字中访问属性名称的值?这样的事情:

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'
})


推荐答案

不,无法从定义本身访问当前正在定义的对象文字。

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.

这篇关于在同一对象中访问JavaScript Object Literal值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆