Hi i have object which has container element and same subElement:
var Obj = function(){
this.container = $('.obj');
this.subObjs = container.find('.subObj');
}
In this Obj i want create another Object(ObjHandler) who ad handlerOn click for each sub object.
var Obj = function(){
this.container = $('.obj');
this.subObjs = container.find('.subObj');
this.init = function() {
this.subObjs.each(function(i,el){
el = new this.ObjHandler(el)
});
}
this.ObjHandler = function(element) {
this.el = element;
this.element.on('click',function(){
console.log('click');
});
}
}
I know i cam make it simple way like this.subObjs.on('click', function(){})
but i this is only simplification.
here is on codepen.io
Is it good idea and why its no works?
ObjHandler(el)withinsubObjs.each. To overcome that issue; just createvar self = this;at first line inthis.init() {function and then use that variable inside.each(likeel = new self.ObjHandler(el).