I need to set a json object property in a object, then reference that property from another object in the same object [duplicate]

大兔子大兔子 提交于 2021-01-29 06:40:27

问题


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]
    }
}

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!


回答1:


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.

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;



回答2:


Then src must be a function.

var object = {
    ext: {
        sound: '.mp3'
        },
    pref: {
        acc: 1
    },
    StAd0: {
        from: 25,
        to: 180,
        src: function() {
            return '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]
    }
}

But you must get src like this,

object.stAd0.src();

EDIT:

There isn't anyway to do it without a function, because if you try that like this,

src: ar/55871+ this.ext.sound;

that will work only once at parse-time. Than it will have a static value inside.

If you can't change all src properties to function, you can get it like this as well.

var src = (typeof object.StAd0.src == "function")? object.StAd0.src() : object.StAd0.src;

So If src is defined as a function, it will call it else will take it like a property.




回答3:


This should work, give it a try:

var object = {
  ext: { sound: '.mp3' },
  StAd0: { src: 'ar/55871'+ object.ext.sound }
}


来源:https://stackoverflow.com/questions/16492703/i-need-to-set-a-json-object-property-in-a-object-then-reference-that-property-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!