I want to create a javascript library, so I thought that making it an instantly self-executing function would be nice to do to ensure the security of the area and thatβs it.
But now I have a problem using the keyword "this", which I do not quite understand.
How can I make code like this work correctly? He is currently telling me that the "image" is undefined.
(function() {
function lib() {
this.image = document.getElementById("image");
this.parts = [
{name: "part1", num: "1"}
];
this.init = function() {
$(parts).each(function() {
var partNum = this.num;
image.getElementById(partNum).addEventListener("click", function() {
toggleHighlight(partNum);
}, true);
});
};
}
window.lib = new lib();
})();
window.lib.init();
How can I access the property image?
source
share