Я пытаюсь понять этот кусок 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;
};
Я не могу понять, что создается новый и, следовательно, пустой объект Image, затем к изображению добавляется метод onload, а затем image.src сбрасывается вручную, если он не запускает перезагрузку. Но зачем это вообще перезагружать? Это просто пустой объект изображения, не имеющий никакого отношения ни к чему. Это как-то автоматически связано с currentObject?
Это код с моими комментариями, посмотрите, помогает ли он. Src должен быть установлен для ожидания события
//this is a jquery plugin,
//jquery plugins start this way
//so you could select $('img').imageCrop();
$.fn.imageCrop = function(customOptions) {
//Iterate over each object
this.each(function() {
//keeps the current iterated object in a variable
//current image will be kept in this var untill the next loop
var currentObject = this,
//creates a new Image object
image = new Image();
// And attach imageCrop when the object is loaded
//correct
image.onload = function() {
$.imageCrop(currentObject, customOptions);
};
//sets the src to wait for the onload event up here ^
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;
};