2

I would like to place all of JavaScript DOM element queries in an object and access them throughout my script. Here's the current design pattern I'm using and I would like to stick to this format if possible:

(function ($) {

EXAMPLE = {

    basicExample : function () {

        config : {
            logo : $('#logo'),
            footer : $('footer'),
        },

        EXAMPLE.config.logo.hover(function () {
            $(this).addClass('example');
        }, function () {
            $(this).removeClass('example');
        });
}

EXAMPLE.basicExample();

})(jQuery);

Accessing the logo DOM element doesn't seem to work like this: EXAMPLE.config.logo

1
  • 1
    config isn't a property of EXAMPLE. It's not even a property or variable of basicExample - it's a label (you used : instead of =). Commented May 21, 2013 at 17:49

6 Answers 6

4

You did misplace the config part - not in your EXAMPLE object literal, but inside your basicExample function (where it acted as a labelled block statement with no-op expression statements inside, so it threw no error). Instead, it should be

(function ($) {
    EXAMPLE = {
        config : {
            logo : $('#logo'),
            footer : $('footer'),
        },
        basicExample : function () {
            EXAMPLE.config.logo.hover(function () {
                $(this).addClass('example');
            }, function () {
                $(this).removeClass('example');
            });
        }
    };
    EXAMPLE.basicExample();
})(jQuery);

However, you will need to place the initialisation into a DOM-ready handler, otherwise it might not find the elements. So either use

EXAMPLE = {
    init : function($) {
        EXAMPLE.config = {
            logo : $('#logo'),
            footer : $('footer'),
        };
        EXAMPLE.basicExample();
    },
    basicExample : function() {
        this.config.logo.hover(function () {
            jQuery(this).addClass('example');
        }, function () {
            jQuery(this).removeClass('example');
        });
    }
};
jQuery(EXAMPLE.init);

or just put everything in the handler, without any module pattern and extra basicExample function:

jQuery(function ($) {
    var config = {
        logo : $('#logo'),
        footer : $('footer'),
    };
    config.logo.hover(function () {
        $(this).addClass('example');
    }, function () {
        $(this).removeClass('example');
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

could also work, just not sure if that's what he ment, or was just using EXAMPLE as a namespace..one or the other ;)
1

You are using object literal notation to define an object, and inside that object you define a constructor function, that needs to be used via new to be useful..I believe what you wanted is to create a namespace with a single object inside it.

try to remove the function and you should be able to access it, hence:

var EXAMPLE = {
   basicExample : {
     config : {
         logo : $('#logo')
     }
  }
}

Comments

1

I would suggest declare a global object like this:

EXAMPLE = {
    basicExample: function () {
        this.config = {
            logo: $('#logo'),
            footer: $('footer')
        };
        return this;
    },
    applyHover: function () {
        this.config.logo.hover(function () {
            $(this).addClass('example');
        }, function () {
            $(this).removeClass('example');
        });
    }
};

And then call a .basicExample().applyHover() on document ready.
FIDDLE EXAMPLE

Comments

0

You have a comma at the end of the config.

config : {
            logo : $('#logo'),
            footer : $('footer'),
        },

Should be:

config : {
            logo : $('#logo'),
            footer : $('footer')
        },

4 Comments

Since the official death of IE7, extra comma shouldn't be a problem anymore
@roasted Sorry, no official death of IE7
IE7 ships with windows XP, it will never die! though this answer should have probably been a comment, but he doesn't have the rep (yet).
@roasted Yeah seriously...I'm tired of "having to support it"
-1

You can do it like this:

var EXAMPLE = {
   basicExample : {
     config : {
         logo : $('#logo')
     }
  }
}
EXAMPLE.basicExample.config.logo.hover(function () {
            $(this).addClass('example');
        }, function () {
            $(this).removeClass('example');
        });

JsFiddle

Or you can do what Artyom suggested.

1 Comment

Why not use the hover method like OP did?
-2

EXAMPLE.basicExample is a function this is wrapping your config parameter. That is why it is not available to EXAMPLE.config. If you want the function EXAMPLE.basicEXAMPLE to define config as an element of EXAMPLE then do this:

EXAMPLE = {

    basicExample : function () {

        EXAMPLE.config = {
            logo : $('#logo'),
            footer : $('footer'),
        },

        EXAMPLE.config.logo.hover(function () {
            $(this).addClass('example');
        }, function () {
            $(this).removeClass('example');
        });
    }
}

4 Comments

Why the downvote? I even tested this and it works and answers the question.
No, it does not work. It still has the same mistake (not my downvote, though).
Yeah, I tested this as well and it does not work (also not my downvote)
Ok it was a typo missing = in place of the :. But the key was that it declares EXAMPLE.config instead of config. And it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.