Javascript storing properties and functions in variables

Posted by richard on Stack Overflow See other posts from Stack Overflow or by richard
Published on 2010-04-26T17:42:06Z Indexed on 2010/04/26 17:43 UTC
Read the original article Hit count: 236

Hello,

I'm having trouble with my programming style and I hope to get some feedback here. I recently bought Javascript: The Good Parts and while I find this a big help, I'm still having trouble designing this application.

Especially when it comes to writing function and methods. Example:

I have a function that let's the user switches games in my app. This function updates game-specific information in the current view.

var games = {
    active: Titanium.App.Properties.getString('active_game'),

    gameswitcher_positions: {
        'Game 1': 0,
        'Game 2': 1,
        'Game 3': 2,
        'Game 4': 3,
        'Game 5': 4 
    },

    change: function(game) {
        if (active_game !== game) {
            gameswitcher.children[this.gameswitcher_positions[this.active]].backgroundImage = gameswitcher.children[this.gameswitcher_positions[this.active]].backgroundImage.replace('_selected', '');

            gameswitcher.children[this.gameswitcher_positions[game]].backgroundImage = gameswitcher.children[this.gameswitcher_positions[game]].backgroundImage.replace('.png', '_selected.png');

            events.update(game);

            this.active = game;
        }
    },

    init: function() {
        gameswitcher.children[this.gameswitcher_positions[this.active]].backgroundImage = gameswitcher.children[this.gameswitcher_positions[this.active]].backgroundImage.replace('.png', '_selected.png');

        events.update(this.active);
    }
};

gameswitcher is a container view which contains buttons to switch games.


I am not satisfied with this approach but I cannot think of a better one. Should I place the gameswitcher_positions outside of the variable in a seperate variable instead of as a property? And what about the active game? Please give me feedback, what am I doing wrong?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about methods