I want to make set with for my own objects witch can recognize same objects at append and falls them.
function ImageSet(){
this.set = [];
}
ImageSet.prototype.length = function(){return this.set.length}
ImageSet.prototype.process = function(imgObj){
var is_exist = false;
$.each(this.set, function(i){
if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
});
if (!is_exist){
this.set[this.set.length + 1] = imgObj;
return true;
}
return undefined;
}
var imagesChoosen = new ImageSet();
And so when user click on element I create new object and try append it to my ImageSet.
On the first click object successfully append to set, but on second click console write error
TypeError: this.set is undefined if (this.set[i].imgHash == imgObj.imgHash){is_exist = true};
And code which call ImageSet is:
$('#content_table a').click(function(event){
event.preventDefault();
el = new Image($(this).attr('href'));
if (imagesChoosen.process(el)){
$(this).parent().css("border", "3px dotted orange");
} else {
$(this).parent().css("border", "None");
}
console.log(imagesChoosen.length());
return false;
});
I can’t understand. Is my ImageSet is destruct after the first call?
think should be:
I am not familiar with the JQuery each function but previous posts would indicate changing of this context so I would change the for each:
to: