Skip to content Skip to sidebar Skip to footer

Create An Audio Element And Give It Properties Inside An Object Literal

I'm trying to get out of bad habits of writing spaghetti code. I found a great tutorial on modular javascript and I'm trying to apply it to a project of mine. I have this: var sfx

Solution 1:

Ya, you wouldn't be able to create the element and change it's properties in an object definition. I think your original way of doing it is just fine.

If you really wanted to, you could:

const sfx = {
    mySound1: document.createElement('audio'),
    mySound2: document.createElement('audio')
}

sfx.mySound1.src = 'https://some-url.ogg';
sfx.mySound1.moreProps = "...";

sfx.mySound2.src = 'https://some-url.ogg';
sfx.mySound2.moreProps = "...";

You could get weird with it and set the properties in separate objects and then use those objects to override the properties of the element object, but that would be bad weird, not good weird ;)

Post a Comment for "Create An Audio Element And Give It Properties Inside An Object Literal"