I Need To Set A Json Object Property In A Object, Then Reference That Property From Another Object In The Same Object
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
Solution 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;
Solution 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.
Solution 3:
This should work, give it a try:
var object = {
ext: { sound: '.mp3' },
StAd0: { src: 'ar/55871'+ object.ext.sound }
}
Post a Comment for "I Need To Set A Json Object Property In A Object, Then Reference That Property From Another Object In The Same Object"