jquery - How to flatten a touchmove listener in JavaScript? -


trickshot #29 shows how define touch events in jquery. i've reworked style of rogue writing in this fiddle.

what author define touchmove listener whenever touchstart event fired.

request.dom.ball.on('mousedown touchstart',mytouchstart); function mytouchstart(myevent){    request.dom.ball.on('mousemove.mynamespace touchmove.mynamespace',mytouchmove);    function mytouchmove(myevent) { 

what i'd put mytouchmove outside of mytouchstart because rogue style of javascript writing try keep flat possible, , not have functions inside of functions, if can it.

that might seem strange since wrap inside of:

(function() { })(); 

to begin with, don't want have functions inside of functions inside of functions if can it.

one way return inner function function (called closure) pass variables referencing original function.

as far can tell in case that's 1 variable - local - if ever add new variables, add new argument function definition , function call.

updated fiddle: http://jsfiddle.net/ynuhb/1/

the affected code:

request.dom.ball.on('mousedown touchstart',mytouchstart);  function mytouchstart(myevent){     var local = {};     //...     request.dom.ball.on('mousemove.mynamespace touchmove.mynamespace',mytouchmove(local));     //... };  function mytouchmove(local) {     return function(myevent) {         var mycss = {};          myevent = (myevent.originalevent.touches) ? myevent.originalevent.touches[0] : myevent;         mycss.top = local.elementposition.y + myevent.pagey - local.startposition.y;         request.dom.top.text(mycss.top);         mycss.left = local.elementposition.x + myevent.pagex - local.startposition.x;         request.dom.left.text(mycss.left);         request.dom.ball.css(mycss);     }; }; 

see:


Comments