javascript - Recursively call function returned as object -


i have following piece of code:

// core zoom logic, independent of event listeners. $.zoom = function(target, source, img) {     var outerwidth,         outerheight,         xratio,         yratio,         offset,         position = $(target).css('position');      // part of code omitted      return {         init: function() {             outerwidth = $(target).outerwidth();             outerheight = $(target).outerheight();             xratio = (img.width - outerwidth) / $(source).outerwidth();             yratio = (img.height - outerheight) / $(source).outerheight();             offset = $(source).offset();         },         move: function (e) {             var left = (e.pagex - offset.left),                 top = (e.pagey - offset.top);              top = math.max(math.min(top, outerheight), 0);             left = math.max(math.min(left, outerwidth), 0);              img.style.left = (left * -xratio) + 'px';             img.style.top = (top * -yratio) + 'px';         },          automove: function() {             // can recall this?          }      }; }; 

what want achieve add following effect in automove() function:

$(img).animate({   top: newtop,   left: newleft,   }, 1000, function() {     automove(); /* recall */   }); 

but how call automove again it's body? maybe should change way functions declared in $.zoom function?

if want recursively call automove() inside traditional method use arguments.callee. code like:

return {      /* ... */      automove: function() {         $(img).animate({           top: newtop,           left: newleft,         }, 1000,         arguments.callee /* recall */       );      } } 

but in html5 deprecated , illegal in strict mode. instead can give function name:

return {      /* ... */      automove: function myautomove () { // <-- give name        $(img).animate({           top: newtop,           left: newleft,         }, 1000,         myautomove /* recall */       );      } } 

named function expression works in browsers old , new , easier read.


note:

if function not require parameters can pass reference callback instead of wrapping in anonymous function:

settimeout(function(){ foo() },100); // <-- unnecessary  settimeout(foo,100); // <-- need instead 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -