0

Is it possible to store jQuery objects inside Object Literal pattern? In my case inside config.

var myApp = {
    config: {
        disabledElem: $('.disable')
    },

    init: function () {
        someFunction(this.config.disabledElem);
    }
};

$(function () {
    myApp.init();
});
2
  • 2
    did you try it? what is the problem you faced? Commented Apr 20, 2016 at 12:33
  • as long as the jquery and element has already been loaded by the time this myApp is initialized, it will work. Commented Apr 20, 2016 at 12:33

1 Answer 1

2

Yes, you can absolutely store a reference as a value in an object literal.

Perhaps some of your confusion is how you are accessing this value in your init function.

Try something like this instead:

init: function () {
    someFunction(myApp.config.disabledElem);
}
Sign up to request clarification or add additional context in comments.

6 Comments

this inside init method is already referring to myApp object
@A.Wolff but it's not clear from the question how the init function is being invoked so it's not clear what this will be during invocation
@Jonathan.Brink and how can I call the stored object inside the config ? For e.g. disabledInput: disabledElem.closest('input')
@Gabriel you mean the dom node? $('.disable').get(0)
@Jonathan.Brink Yes, the DOM node. Currently I have duplicated nodes: config: { form__internet: $('.form__internet'), fi__parent: $('.form__internet').closest('.fieldTypeINPUT') }
|

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.