我需要在一个对象中设置一个 json 对象属性,然后从同一对象中的另一个对象引用该属性 [英] I need to set a json object property in a object, then reference that property from another object in the same object

查看:62
本文介绍了我需要在一个对象中设置一个 json 对象属性,然后从同一对象中的另一个对象引用该属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我希望能够根据浏览器更改正在使用的音频.我有一个如下所示的对象,它有一个ext: { sound: '.mp3' }",在某些时候我会在浏览器之间做一些区分,然后使用类似object.ext.sound = '.ogg'的东西" 根据正在使用的浏览器设置新的声音类型.我如何从对象内部引用该变量,例如StAd0"?

I have this issue, I want to be able to change the audio being used based on the browser. What I have is an object seen below, which has a "ext: { sound: '.mp3' }", at some point I will make some distinctions between browser then use something like "object.ext.sound = '.ogg'" to set the new sound type based off the browser being used. How do I reference that variable from within the object in for instance "StAd0"?

var object = {
    ext: {
        sound: '.mp3'
        },
    pref: {
        acc: 1
    },
    StAd0: {
        from: 25,
        to: 180,
        src: 'ar/55871'+ this.ext.sound,
        du: 5228
    },
    Image_33: {
        type: 15,
        from: 4,
        to: 48,
        rp: 0,
        rpa: 0,
        mdi: 'Image_33c',
        trin: 6,
        trout: 6,
        ef: {}
    },
    Image_33c: {
        b: [171, 259, 850, 360],
        ip: 'dr/7029_679_101.png',
        dn: 'Image_33',
        visible: 1,
        accstr: 'Image ',
        vb: [171, 259, 850, 360]
    }
}

我现在拥有的东西一直说this.ext.sound"的值是undefined".我认为它与范围有关,但老实说我很难过,我觉得我已经尝试了点符号的每种组合来尝试获取该属性,但我只是没有正确引用该属性.

The way I have things now it keeps saying that "this.ext.sound" has a value of "undefined". I was thinking it has to do with scope but quite honestly I am stumped, I feel like I've tried every combination of dot notation to try to get the property but I am just not referencing the property correctly.

非常感谢任何帮助,提前致谢!

Any help is greatly appreciated, thanks in advance!

推荐答案

您尝试做的事情是不可能的.

what your trying to do is not possible.

必须先初始化对象,然后才能引用其值

the object has to be initialized before its values can be referred to

你可以在对象内部使用一个函数.

you could use a function inside the object.

或将 StAd0.src 的初始值更改为 '''ar/55871' 然后在设置后重置该值var object; to object.StAd0.src = 'ar/55871'+ object.ext.sound;

or change the initial val of StAd0.src to '' or 'ar/55871' then reset that value after you set the var object; to object.StAd0.src = 'ar/55871'+ object.ext.sound;

var object = {
ext: {
    sound: '.mp3'
    },
pref: {
    acc: 1
},
StAd0: {
    from: 25,
    to: 180,
    src: 'ar/55871',
    du: 5228
},
Image_33: {
    type: 15,
    from: 4,
    to: 48,
    rp: 0,
    rpa: 0,
    mdi: 'Image_33c',
    trin: 6,
    trout: 6,
    ef: {}
},
Image_33c: {
    b: [171, 259, 850, 360],
    ip: 'dr/7029_679_101.png',
    dn: 'Image_33',
    visible: 1,
    accstr: 'Image ',
    vb: [171, 259, 850, 360]
}
};
 object.StAd0.src = 'ar/55871'+ object.ext.sound;

这篇关于我需要在一个对象中设置一个 json 对象属性,然后从同一对象中的另一个对象引用该属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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