I'm trying to understand this piece of JQuery.
$.fn.imageCrop = function(customOptions) { //Iterate over each object this.each(function() { var currentObject = this, image = new Image(); // And attach imageCrop when the object is loaded image.onload = function() { $.imageCrop(currentObject, customOptions); }; // Reset the src because cached images don't fire load sometimes image.src = currentObject.src; }); // Unless the plug-in is returning an intrinsic value, always have the // function return the 'this' keyword to maintain chainability return this; }; What I can't understand is that a new and therefore empty Image object is created, then an onload method is added to the image and then the image.src is manually reset in case it doesn't fire a reload. But why would it fire a reload at all? It's just an empty image object with no relation to anything. Is is somehow automatically linked to currentObject?
$.imageCropneeds the image to be loaded, so it is executed when the image object is loaded. The comment above the line that setssrcis not clear enough. It should explain that you setsrcthere, and not beforeonloadbecause if the image has previously been loaded (and is therefore cached),onloadmay not fire. Settingsrcafter you create theonloadhandler stops that happening. It's just an unclear (bad) comment.