javascript - Jquery in 30 days inquiry -


i watching tutorial video "30 days learn jquery". have question why tutor in video returned variable function.

here's code:

this in html file binds event handler buttons, calls functions, etc.

(function() {      slider.nav.find('button').on('click', function() {         slider.setcurrent( $(this).data('dir') );         slider.transition();     });      })(); 

and 1 function i'm interested in (in js file):

slider.prototype.setcurrent = function( dir ) {     var pos = this.current;      pos += ( ~~( dir === 'next' ) || -1 );     this.current = ( pos < 0 ) ? this.imgslen - 1 : pos % this.imgslen;      return pos; // <== here  }; 

the thing want figure out why return pos? tried removing , code still worked.

was mistake or there sound logic this?

in nutshell, setcurrent function called , setcurrent returns value. why?

it's hard know without seeing rest of code, functions set value on object return something, though counterintuitive, since purpose set value, not something.

the common pattern you'll see return object itself. allows chain multiple setter calls @ once:

object.setcolor("red").setsize("large"); 

this not going on in case, author may have had similar use in mind, in sense wanted 2 things @ once: set value, , information how value set.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -