I've created some kind of JavaScript object with the such design pattern:
var MyObject = (function(){
var element;
var config = {
// Defaults...
};
function initialize(id, options){
element = $(id);
config = $.extend(config, options);
}
function method(){
// Some code...
}
element.on('click', function(){
// Some actions when clicked on element...
});
return {
initialize: initialize,
// Other public methods...
};
})();
And that's how object is initialized:
MyObject.initialize('#someId');
Script works great, however an error occurs, when I try to add some events for element
.
As I realized, an anonymous function (function(){ ... })();
is executed immediatelly, but initialization of variable element
comes after.
So, how can I implement an event handling exactly for this JavaScript object pattern?
Thanks!