1

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!

rene
  • 41,474
  • 78
  • 114
  • 152
Tom Bird
  • 999
  • 3
  • 17
  • 31
  • http://stackoverflow.com/a/2787260/594589 – dm03514 May 11 '13 at 00:58
  • None of these do what I need, unfortunately it doesn't look like from what I read a way to reference a javascript literal inside the same literal. The reason it has to be from within is because the client is using Adobe Captivate which publishes the projects.js which has this javascript object literal. – Tom Bird May 11 '13 at 02:42

3 Answers3

2

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;

Jay Harris
  • 4,201
  • 17
  • 21
  • Ya thats what I figured, what sucks is that now I would have to manually go through write a script to find all the 75 sounds files, loop through them, split them and then place the new extension back in. It just seemed like there should be some easier way, but yes after looking for a bit I figured I would leave this open before giving up hope! Thanks! – Tom Bird May 11 '13 at 07:34
1

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.

Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • So there isn't a way to just add a variable that references to the json object "ext" and replace the file extension with that? I can't use a function, there are 20 of these courses with 75 instances of this extension and the program is built to just grab these properties in an automated way and if I break out of that it will break the code. Is there any other way? – Tom Bird May 11 '13 at 02:18
  • @TomBird, Nope, look at my upated answer. – Okan Kocyigit May 11 '13 at 07:42
-5

This should work, give it a try:

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